Search code examples
apachemod-rewriteurl-rewritingwampserver

Using RewriteRule to redirect any request to PHP script without external redirects


I use WampServer (Apache/PHP/MySQL) and I have the following files in my www directory:

  • www [dir]
    • test [dir]
    • index.php [file]
    • .htaccess [file]

I want to pass all URI requests as a parameter to index.php without causing any external redirects (location changes in client browser). So I tried to use the following rule:

RewriteRule ^(.*)$ index.php?path=$1 [nocase,qsappend,end]

Which works quite good, except when the URI is /test, in which case an external redirect changes the location to:

/test/?path=test

How can I make sure the external redirect does NOT happen even if the directory of the same name as the URI exists?


Expected result:

  • Original URI request: /test
  • Internal redirect: index.php?path=test
  • External redirect: none

Actual result:

  • Original URI request: /test
  • Internal redirect: index.php?path=test
  • External redirect: /test/?path=test

The ideal solution would be universal and would not apply only to test directory, but to any directory.


Solution

  • Ok the problem seems to be a conflict between mod_rewrite and mod_dir. Possible solutions are:

    1. Disable mod_dir
    2. Put this to .htaccess:

      <IfModule mod_dir.c>
           DirectorySlash Off
      </IfModule>
      

    For more details about how mod_rewrite and mod_dir interact, this answer has some excellent info. And why the external redirect is necessary is explained in docs (Trailing Slash Problem).


    Related links