I've just installed a fresh apache (Apache/2.4.7 - Ubuntu) and created and enabled a site with a Document Root of /var/www/html. With mod_rewrite enabled, I proceeded to create a simple .htaccess file in the that directory. I then added the standard:

Options +FollowSymlinks
RewriteEngine on 

Then I started creating a RewriteRule. Since .htaccess is usually not terribly friendly, I typically create redirects by first sending the result as a parameter to a temporary php file which just dumps $_GET so that I can see what is happening. So my rewrite rule starts from the basic:

RewriteRule ^(.*)$ test.php?name=$1

And my test request: /test/path.html

Expected result: [name] => test/path.html Actual result: [name] => test.php WTH?

It's as if a 301 redirect had already somehow taken place. I seem to have no way to get the actual request path. Moreover, when checking server variables that should have the path like %{PATH_INFO}, %{ORIG_PATH_INFO}, they come up empty and %{REQUEST_URI} returns /test.php.

I've tried adding AcceptPathInfo On to the .htaccess but that has no effect. I've also tried adding RewriteBase / but this also has no effect. Also, I'm getting the same results after moving the rewrite rule to my .conf file.

Any idea what might be going on here?


RewriteRules in .htaccess "ignore" all the preceding path elements:

In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that led the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).

so you may want to add

RewriteBase /test

to your .htaccess. If you want to bypass that - configure rewrites on Vhost/httpd.conf level.

Just tested with following configuration

# cat .htaccess 
RewriteEngine on
RewriteBase /~dimon
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(rw.*)$ test.php?name=$1

# cat test.php 
<? echo "Name:",$_GET["name"]; 

and upon requesting http://myserver.com/~dimon/rwfoo, get:

REDIRECT_SCRIPT_URI http://myserver.com/~dimon/rwfoo
SCRIPT_URL  /~dimon/rwfoo
SCRIPT_URI  http://myserver.com/~dimon/rwfoo
REQUEST_URI /~dimon/rwfoo
SCRIPT_NAME /~dimon/test.php
  • It doesn't matter what I provide in the test url. The output does not change. I just tried, for example //helloworld and //hello.html. The output is still "test.php". It is not rewriting with the request info. – Chadananda Apr 10 '15 at 19:57
  • please read carefully my reply - RewriteRule inside .htaccess ignores path elements. So RewriteBase helps with that for rewrites but it will not show in your PHP script. – Droopy4096 Apr 10 '15 at 20:39
  • Yes, I saw that which is why I mentioned that I also tried requests for fake files in the web root folder. Among the things I tried was to set a RewriteBase /. It made no difference. Notice the %{REQUEST_URI} returns the target php file rather than the request uri. Something else is going on here. – Chadananda Apr 10 '15 at 21:21
  • move .htaccess to the DocumentRoot and it'll work as you expect it. Otherwise - documentation states clearly that what you'd like to see is impossible. – Droopy4096 Apr 10 '15 at 21:25
  • Sorry if I was not clear enough, the .htaccess and the test.php files are in the Document Root directory (/var/www/html/). – Chadananda Apr 10 '15 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.