I’m trying to clean up my URLs using mod_rewrite in my .htaccess files.
Let’s say I have the following files on my server:
/about.php
/contact.php
If a user types about or contact, I want about.php or contact.php to be displayed to the user, but ensuring the URL stays the same (i.e. it all happens server-side). Similarly, if the user enters about.php or contact.php, I want it to 301 redirect to about or contact, which will then pull the page server-side, whilst keeping a pretty URL.
What I have tried so far:
RewriteEngine on
RewriteRule about about.php
RewriteRule contact contact.php
RewriteRule about.php about [R]
RewriteRule contact.php contact [R]
That results in a circular redirect loop.
And the other way round:
RewriteEngine on
RewriteRule about.php about [R]
RewriteRule contact.php contact [R]
RewriteRule about about.php
RewriteRule contact contact.php
This is functional, but the redirect doesn’t work (the URL stays the same).
How can I make it so that I don’t get a redirect loop but my pages will all point to the version without .php?
Note: I know that [R] is 302 not 301 but I’m using 302 for testing.
1 Answer