Forum Moderators: phranque

Message Too Old, No Replies

ReWrite rule to add .html extension

         

S_Combs

12:27 pm on Apr 21, 2011 (gmt 0)

10+ Year Member



I need a rule that will add an .html extension whenever there is 'not' a trailing slash if this is possible.

I have a new client that has changed ecommerce scripts and, the new version handles SEO differently and changed all of their 16,000+ product links. This was not caught prior to the site being re-indexed so it's now a big problem and I need a little help with this.

All of their products used to have links like this
domain.com/category/productname
but are now
domain.com/category/productname.html

Category links did not change and all are like this
domain.com/category/ (with trailing slash)

I tried a variety of things but couldn't figure out how to limit the redirect only to links without a trailing slash. I then found an old thread here and the bottom replies seemed to be exactly what I'm needing but are not working.
[webmasterworld.com...]

These are the most promising methods I have tried
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) /$1.html [L]

RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule ^([^.]*[^./])$ $1.html [L]

RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule ^([^./]+)$ $1.html [L]


Existing rules from cart script
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]


Thanks for helping if you can

S_Combs

4:27 pm on Apr 21, 2011 (gmt 0)

10+ Year Member



I was able to resolve this problem using this rule

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule ^(.*)$ /$1.html [R=301,L]

g1smd

6:00 pm on Apr 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You would have been far better off changing the new site to use extensionless URLs and then redirecting recently indexed new .html URLs to the extensionless version.

jdMorgan

6:30 pm on Apr 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



More efficiently-coded version of current solution (see important comment in post above):

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]*[^./])$ /$1.html [R=301,L]

Jim