Reputation: 6846
I have the following file structure:
/framework
/.htaccess
/index.php
and the following rules in my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ index.php?q=$1 [L]
</IfModule>
When I navigate to http://localhost/framework/example
I would expect the query string to equal 'framework/example' but instead it equals 'index.php'. Why? And how do I get the variable to equal when I'm expecting it to?
Upvotes: 0
Views: 830
Reputation: 143906
Your rewrite rules are looping. Mod_rewrite won't stop rewriting until the URI (without the query string) is the same before and after it goes through the rules. When you originally request http://localhost/framework/example this is what happens:
/framework/example
and strips the leading "/"framework/example
is put through the rulesframework/example
gets rewritten to index.php?q=framework/example
framework/example
!= index.php
index.php?q=framework/example
goes back through the rewrite rulesindex.php
gets rewritten to index.php?q=index.php
index.php
== index.php
index.php?q=index.php
You need to add a condition so that it won't rewrite the same URI twice:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ index.php?q=$1 [L]
Upvotes: 3
Reputation: 254944
Because you've rewritten the url with RewriteRule
and have already put the previous path to the q
. So just use $_GET['q']
Upvotes: 3