Forum Moderators: phranque

Message Too Old, No Replies

redirect to www & rewrite extension

         

jon86mtl

5:10 am on Mar 2, 2009 (gmt 0)

10+ Year Member



I am trying to automatically redirect all pages from non-www urls to www urls (example.com to www.example.com). Additionally, I would like to remove the .html extension from all the pages (www.example.com/page.html to www.example.com/page). I cannot get both to work at the same time. If I type example.com/page it redirects to www.example.com/page.html.

my code:

RewriteEngine On
RewriteBase /
#
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
#
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [R=301,L]

Any help would be appreciated.

g1smd

11:35 am on Mar 2, 2009 (gmt 0)

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



Add
,L
to the
[R=301]
on the first rule, making it into
[R=301,L]
.

Remove the

R=301
from the last rule to change it from a redirect into being a rewrite. Use only
[L]
there.

Additionally

RewriteBase / 
is the default and can be removed.

Finally,

^(.*)$
simplifies to
(.*)
here.

You also need to link to the URLs you want users to 'see' from within the pages of your site.

You were very close! That's one of the best "first tries" posted into the forum in recent weeks.

You will also need to set up a 301 redirect such that if anyone asks for a .html URL they are redirected to the correct URL. Force www within the same redirect for those requests too. That new, extra, redirect code goes before your existing non-www to www redirect code.

jon86mtl

5:00 pm on Mar 2, 2009 (gmt 0)

10+ Year Member



Thanks for the help, I'm definitely getting closer.
My new code:

RewriteEngine On
#
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [L]

I still must be doing something wrong, because example.com/page still changes to www.example.com/page.html instead of www.example.com/page

g1smd

5:37 pm on Mar 2, 2009 (gmt 0)

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



Flush your browser cache and try again.

jon86mtl

5:52 pm on Mar 2, 2009 (gmt 0)

10+ Year Member



nope... still the same result.