Sam
Sam

Reputation: 19057

Making a rewriterule remove .php extension?

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?

Upvotes: 2

Views: 6205

Answers (1)

andri
andri

Reputation: 11292

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.

Upvotes: 3

Related Questions