Link to home
Start Free TrialLog in
Avatar of xill2678
xill2678

asked on

removing page extension using mod_rewrite

ok i have successfully removed my pages' extensions using mod_rewrite, but now I'd like to take it a step further. Right now, a page named "domain.com/example.php" is linked from my other pages as "domain.com/extension" and it works. If someone mistakenly links to the page with the trailing slash (ex: domain.com/example/) then my htaccess file removes the trailing slash from the url (redirecting to the no trailing slash version). The problem I have now is if the page is accessed with the extension (domain.com/example.php) it does not redirect to the correct format (no extension & no slash). Basically creating duplicate content in the eyes of Google. I know this isn't a big deal since no one will know the extension of the page, but better safe than sorry. Is there another htaccess snippet i can use to force the extension removal of every page (without doing this for each individual page) or a line of php i can insert into the header of the pages that redirects it to the correct format? thanks in advance. my current htaccess code is below...
Options +FollowSymlinks
RewriteEngine on
 
# canonical domain rewrites
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ http://www.domain.com/ [R=301,L]
 
# remove extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

Open in new window

Avatar of ahoffmann
ahoffmann
Flag of Germany image

RewriteCond %{REQUEST_URI} !\.php$
Avatar of xill2678
xill2678

ASKER

this snippet doesnt seem to be doing anything for me. is it suppose to replace something?
Avatar of David S.
It won't do anything by itself, since it's just a condition.

Try this:
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^(.*)\.php$ http://www.domain.com/$1 [R=301,L]

Open in new window

i tried adding this to what i have, and i tried replacing what i have with this snippet. one way removed the extension but caused a 404 error. the other way resulted in a redirect loop :(

i'm new to rewrites, so i'll post what i have so far. i'm sure it's something small that i'm overlooking or doing wrong. the snippet you gave is the bottom two lines
# remove extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
 
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^(.*)\.php$ http://www.domain.com/$1 [R=301,L]

Open in new window

Does putting it first help?
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^(.*)\.php$ http://www.domain.com/$1 [R=301,L]
 
# remove extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

Open in new window

no, unfortunately that throws it into a redirect loop again
ASKER CERTIFIED SOLUTION
Avatar of David S.
David S.
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that works perfectly!! THANKS :)
> RewriteCond %{REQUEST_URI} \.php$
this may cause a rewrite loop, that's why I used ! for negation:)
I'm wondering that the accepted suggestion works.