views:

56

answers:

2

My htaccess isn't quite working the way I want it to. I've seen some similar threads on here but they aren't quite what I need and I don't know enough yet about htaccess to modify the code to suit my needs.

This is what I have working so far: I've got all non-www URLs redirecting to www URLs and I'm doing an internal rewrite of all URLs to the corresponding PHP file on the server. In the files I have relative links that are clean without any file extension on them.

This is what I need to do yet: All the pages on my site are still accessible through URLs with .php on the end. For SEO reasons I want the URL's with .php to all do an external 301 redirect to the clean URL without the extension.

Here's what I have in my htaccess file that's in the root folder on my server.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

I'd appreciate any help. Thanks in advance!

A: 

I believe this should work

RewriteRule ^(.*)\.php$ $1 [L,R=301]

L = Last Rule (Apache will stop processing rules if this pattern is matched)

bigstylee
A: 

Thanks bigstylee. I tried adding that to my htaccess file but I'm running into issues. I get this message in my browser when I try to access my website: Firefox has detected that the server is redirecting the request for this address in a way that will never complete. That makes me think I might need a rewrite condition preceding the rewrite rule to keep it from looping.

I'm also having trouble knowing what order to place my rewrite rules in to get them all to apply correctly. For example, if the URL domain.com/products.php is redirected correctly to domain.com/products then I also want the redirect rule I have at the bottom to force it to go to the www.domain.com as well. So the result would be www.domain.com/products

Here's what I have right now.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteRule ^(.*)\.php$ $1 [L,R=301]

RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
spacedatdusk