0

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?

1 Answers1

0

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"]; 
   phpinfo();
?>

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

Name:rwfoo
....
REDIRECT_SCRIPT_URL /~dimon/rwfoo
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
Droopy4096
  • 680
  • 4
  • 8
  • 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
  • @Chadananda just tested with that configuration - it all works as expected. `Name:rwfoo/mooo/cat` in output from URL http://myserver.com/~dimon/rwfoo/mooo/cat – Droopy4096 Apr 10 '15 at 21:36
  • I'm sure it did work for you as expected. But it does not work on my configuration. Your code produces a 404 error since it does not get any path info in order to match `(rw.*)`. When I change the request to `^(.*)$` then always returns `test.php` no mater what request URI I use. Interestingly, when I dump the entire $_SERVER object in PHP, the only variable that contains my request path is REDIRECT_QUERY_STRING. – Chadananda Apr 11 '15 at 00:58
  • that is because you're double-redirecting, and second redirect comes from test.php.... I've included RewriteCond for that purpose in my answer now. – Droopy4096 Apr 11 '15 at 02:34