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?
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/exampleand strips the leading “/”framework/exampleis put through the rulesframework/examplegets rewritten toindex.php?q=framework/exampleframework/example!=index.phpindex.php?q=framework/examplegoes back through the rewrite rulesindex.phpgets rewritten toindex.php?q=index.phpindex.php==index.phpindex.php?q=index.phpYou need to add a condition so that it won’t rewrite the same URI twice: