Hi everyone...
I am just geting to grips with htaccess RewriteRules

I have the following rule that redirects users to website.com/folder

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)\/?$ /webstores/$1/ [L]

But the url is missing the trailing forward slash /
when I try and open php files in the directory, I get the 404 not found error message
when I add the trialing slash myself and open a php file, it opens as expected...

What am I missing....
Thanks

Dani AI

Generated

Short expert note tying the thread together:

The behaviour seen by is expected: an internal rewrite leaves the browser URL unchanged, and when the address bar lacks a trailing slash the browser treats the last path segment as a filename. That changes how relative links and script requests are resolved, which is why manually adding the slash made the PHP files load. ’s redirect fixed this by making the browser request the slash-appended URL.

Practical options and tradeoffs

  • External redirect (recommended when the canonical URL should include a trailing slash): make the server send a 301/302 redirect from the no-slash form to the slash form so the browser requests the directory path explicitly. This fixes relative-path resolution and is SEO-friendly if 301 is used. Place that redirect before other rewrites and exempt real files and existing directories to avoid unwanted redirects.

  • Keep an internal rewrite (if hiding the real path is required): generate absolute links (leading slash) or include a <base> element in pages so asset and script URLs resolve correctly even when the visible URL lacks a trailing slash. This avoids an external redirect but requires changing how links are produced.

  • Let Apache handle it (when real directories exist): DirectorySlash (mod_dir) can add the slash automatically, but it only works when the request actually maps to a filesystem directory.

Troubleshooting checklist

  • Put the trailing-slash redirect before other rules.
  • Exclude requests that contain a file extension or that match existing files/dirs.
  • Test with curl -I to see the redirect header and with browser devtools for asset URLs.
  • Use 301 only when sure (browsers cache 301s).
  • Watch server logs for redirect loops.

Summary: ’s approach is the quick, correct fix for the symptom; deciding between an external redirect or changing link-generation depends on whether the URLs are meant to be canonical directory paths or virtual/internal rewrites.

Recommended Answers

All 4 Replies

You might try this

RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L] 

Hi thanks for replying...
Would I add your suggestion to the existing file or replace it

Hi ...
Thats great that mate thanks...

That worked a treat....

Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.