2

I'm using .htaccess for the first time and I'm encountering a loop problem. I'm trying to achieve the following:

  1. http://something.com rewrites to http://something.com/main

  2. 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!

1 Answer 1

1

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] 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This works exactly as I want it to. The rewrite is for a web app, which dynamically loads content from an external CDN. Intended behavior is for everything to go through index.php in this case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.