Apache/Mod_Rewrite: Redirect and Rewrite without Loop

hankknight
hankknight used Ask the Experts™
on
I want to redirect all pages that have ".html" as the extension to NOT have the ".html" extension and I want to rewrite all pages that do NOT have an extension to point to the page with the ".html" extension.  The problem is that my code creates a loop so does not work.

How can I get this to work without creating a loop?

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_fileNAME} -f
RewriteRule ^(.*)\.html$ $1 [L,R=301]

RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
rewriterule ^(([^/]+/)*[^./]+)/?$ /$1.html [L]
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2005
Commented:
We need to stop the infinite loop, like this

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{ENV:HTMLREDIR} !=1
RewriteRule ^(.*)\.html$ $1 [L,R=301,E=HTMLREDIR:1]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{ENV:HTMLREDIR} !=1
RewriteRule .* %{REQUEST_URI}.html [L,E=HTMLREDIR:1]

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial