2

I have a standard CMS-like RewriteRule set up in my .htaccess:

RewriteRule ^(.+)$ index.php?slug=$1

Let's say I have a directory called foo in the root directory. For some reason, if you hit the page it causes a redirect:

http://example.com/foo redirects to http://example.com/foo?slug=foo

Removing the directory fixes the problem, but unfortunately, it's not an option. Does anyone know of a workaround?

1
  • Since foo is a real directory on the filesystem, by default mod_dir will append a slash, triggering an external redirect (as covener mentions in his answer). I explained more about this in an answer on StackOverflow: stackoverflow.com/a/31701005/369434
    – MrWhite
    Oct 31, 2015 at 11:58

2 Answers 2

2

Try,

DirectorySlash off

It's not the rewrite generating the redirect but mod_dir.

1

Before the RewriteRule put:

RewriteCond %{REQUEST_FILENAME} !-d

This will check if directory foo exists, if it does it won't make rewrite rule.

Also, I'm not sure how you got to foo?slug=foo from redirect destination index.php?slug=$1?


Ok, I've checked, you will need one more ruleset before this one to fix missing /

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ index.php?slug=%{REQUEST_URI} [QSA,L]

This should work like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.