mod_rewrite adds .html when redirecting

Posted by user12093810293812031 on Server Fault See other posts from Server Fault or by user12093810293812031
Published on 2012-03-26T13:05:11Z Indexed on 2012/03/27 5:31 UTC
Read the original article Hit count: 206

I have a redirect situation where the site is part dynamic and part generated .html files.

For example, mysite.com/homepage and mysite.com/products/42 are actually static html files

Whereas other URLs are dynamically generated, like mysite.com/cart

Both mysite.com and www.mysite.com are pointing to the same place. However I want to redirect all of the traffic from mysite.com to www.mysite.com.

I'm so close but I'm running into an issue where Apache is adding .html to the end of my URLs for anything where a static .html file exists - which I don't want.

I want to redirect this:

  http://mysite.com/products/42 

To this:

  http://www.mysite.com/products/42

But Apache is making it this, instead (because 42.html is an actual html file):

  http://www.mysite.com/products/42.html

I don't want that - I want it to redirect to www.mysite.com/products/42

Here's what I started with:

RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]

RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]

I tried making the parameters and the .html optional, but the .html is still getting added on the redirect:

RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]

RewriteRule ^(.*)?(\.html)?$ http://www.mysite.com/$1 [R=301,L]

What am I doing wrong? Really appreciate it :)

© Server Fault or respective owner

Related posts about apache2

Related posts about mod-rewrite