I'm using .htaccess
for the first time and I'm encountering a loop problem. I'm trying to achieve the following:
http://something.com
rewrites to http://something.com/main
http://something.com/anything
rewrites to http://something.com/index.php?page=anything
So far my current attempt looks like this, which works satisfactorily:
RewriteEngine on
RewriteBase /
RewriteRule ^/?$ /main [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?page=$1 [NC,L]
However, I would like to remove both rewrite conditions to also allow requests to http://something.com/index.php
to become http://something.com/index.php?page=index.php
. Removing the two RewriteCond
lines results in a loop and the rewrite doesn't work.
What am I doing wrong and how can I fix the problem? Thanks!
You can remove those conditions but you would need to reverse your rules and remove leading /
from target URIs:
RewriteEngine on
RewriteBase /
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]
RewriteRule ^/?$ main [L]
Also remember that now your first rule will also rewrite css/js/image requests to index.php?page=...
. If you want to avoid that then add this condition before first RewriteRule
:
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|ico|tiff|css|js)$ [NC]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.