Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm very, very (very!) new to using mod rewrite...I have the following set in my htaccess and it works as expected to remove the .php extension. My question is how to also make the url be extensionless if someone puts any extension on the end of the url? Right now http://www.example.com/share etc all work, but so does http://www.example.com/share.php. I'd like to remove any extension on these pages!

Options +FollowSymlinks
RewriteEngine on
# remove php extension from all matching files
RewriteRule ^(share|invite|submission|thankyou|confirm|over|report)$ $1.php [NC]

I've tried several things and it always ends up breaking both functionalities. For some reason this stuff is the most impossible thing to find quick google answers for. Thanks in advance!

UPDATE: Working in a way, but getting infinite redirect loop:

Options +FollowSymlinks
RewriteEngine on
# reslove extensionless urls
RewriteRule ^(share|invite|submission|thankyou|confirm|over|report|emailtest)$ $1.php [NC]
# make .php redirect to extensionless urls
RewriteRule ^(share|invite|submission|thankyou|confirm|over|report).php$ /$1 [R=301,L,QSA]
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Do you mean if someone types .../share.php, you want that to become .../share? If so, you need to redirect it.

RewriteRule ^(share|invite|submission|thankyou|confirm|over|report).php$ /$1 [R=302,L,QSA]

Change that 302 to 301 if it works, to make it do a permanent redirect.

share|improve this answer
    
Man, I was so close on that! This does work but throws me in an infinite redirect loop. I'm sure it's some flag that I don't know about that missing. I pasted the new code above. –  Thomas McCabe Apr 14 '11 at 19:41
    
@Thomas McCabe: The problem with your updated code is order. You are rewriting the URL to end in .php, then saying, "if it ends in .php, redirect" - you need to change that [NC] for [L] and preferably put it after the other RewriteRule. [L] means last, as in this is the last rule that applies, stop rewriting here. –  Orbling Apr 14 '11 at 21:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.