2

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
I fixed my answer, it won't loop now (you have to check with a RewriteCond whether it is already a redirect or not).
It's better to use RewriteCond %{ENV:REDIRECT_STATUS} ="" as this prevents the rule form matching on the internal redirect done for an ErrorDocument directive.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.