views:

1758

answers:

1

Hello, I have come up with a rewriterule to to go to any page on my website without typing in the .php extension because it is automatically added to the url.

The rule is: RewriteRule ^(\w+)/?$ /$1.php

It takes anything you type in my index and adds .php to it, so you can put in http://sampardee.com/index and it pulls up index.php

Now my question is how to detect when a user enters http://sampardee.com/index.php and change it to http://sampardee.com/index

How could I do so with a rewriterule?

A: 

You need to force a redirect on the user without matching the internal redirect:

RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(\w+)\.php$ /$1 [R=301]

RewriteRule ^(\w+)/?$ /$1.php

This redirects the user with a 301 Moved Permanently redirection to the modified URL.

andri
lol, huge loop no matter what.
Sam
You may need to toy around with RewriteCond's then so that both rules won't match at the same time, but the scenario you described should be doable with rewrites.
andri
I fixed my answer, it won't loop now (you have to check with a RewriteCond whether it is already a redirect or not).
andri