Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

migrated from serverfault.com Feb 21 '11 at 21:41

This question came from our site for system and network administrators.

    
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 – w3dk Oct 31 '15 at 11:58

Try,

DirectorySlash off

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.