Tutorial :Unwanted mod_rewrite behaviour



Question:

I just did a fresh install of lamp stack on ubuntu and enabled the mod_rewrite module for my default website. What I want is something similar to the drupal's queries, when the whole query string is kept in one variable. For this purposes the following mod_rewrite code may be used:

RewriteRule ^(.*)$ home.php?q=$1 [L,QSA]  

The problem begins when some a string starts with the name of the file already existing in the directory; For example if I open a page: http://localhost/home/blablabla - the contents of $_GET are as follows:

Array ( [q] => home.php )   

What I want to see is:

Array ( [q] => home/blablabla )   

I think it's something with default website or mod_rewrite configuration, but I just couldn't figure it out...


Solution:1

You have to exclude the home.php:

RewriteCond %{REQUEST_URI} !^/home\.php$  RewriteRule ^(.*)$ home.php?q=$1 [L,QSA]  

Or you exclude every existing file:

RewriteCond %{REQUEST_FILENAME} !-f  RewriteRule ^(.*)$ home.php?q=$1 [L,QSA]  

The reason: The L flag causes an internal redirect with the new rewritten URL. And the new URL home.php is also matched by the expression ^(.*)$.


Solution:2

Not exactly an answer to your question, but isn't that what _SERVER["REQUEST_URI"] and _SERVER["REDIRECT_URL"] are for?


Solution:3

Looks like you might need the recently added [DPI] flag to discard PATH_INFO with multiple per-directory rewrites.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »