Hello,

I was wondering if anyone knew the correct .htaccess rule to accomplish the following:

Link going to website = www.site.com/page.html

Do a permanent redirect from the .htaccess for all URLs ending in .html (using RedirectMatch, I guess) to:

www.site.com/page (no trailing .html)

In other words, cut off the .html and redirect to the correct page.

Thanks

Comments

cog.rusty’s picture

Try this:

RedirectMatch 301 (.*)\.html$ http://www.example.com$1
Interzone’s picture

Thanks for that, but it doesn't seem to work.

We're running into an issue with the existing Drupal rewrite rules:

Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

These essentially override whatever we put into the .htaccess, are these needed or can they be commented?

Not sure how to handle this.

Even using Drupal's suggested:

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

to force www into the URL causes problems on internal page URLs, they come out like:

http://www.site.com/page?index.php?q=page

I've not even been able to remove the backslash (i.e. http://www.site.com/page/ => http://www.site.com/page

Mod Rewrite is like trying to read Sanskrit.

Interzone’s picture

I commented these rules...

#Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_URI} !=/favicon.ico
#RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

and it works fine now...

One more quick question... how to you get rid of the backslash? http://www.site.com/page/ => http://www.site.com/page

Thanks!

Interzone’s picture

I take that back... commenting those causes lots of other issues. Hmmmm.

cog.rusty’s picture

Those Drupal rewrites make "clean URLs" work. (http://example.com/node/15 instead of http://example.com/?q=node/15). They are not needed for anything else.

Try also adding an Options -MultiViews line.

----- Edited to add:

Try

  # Always use www.
  RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
  RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
  
  # Change urlpath.html to urlpath
  RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  RewriteRule ^(.*)\.html$ http://www.example.com/$1 [L,R=301]

Place these before Drupal's final group of rewrites.

Interzone’s picture

Thanks, I tried but I get the same issues.

# Change urlpath.html to urlpath
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)\.html$ http://www.example.com/$1 [L,R=301]

Doesn't seem to work

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

This works fine to force the www, but causes the URLs to become unfriendly.

I wonder if this is even possible with Drupal, while keeping friendly URLs, It seems like one has to give us something.

cog.rusty’s picture

I tested my last suggestion. It works fine for me. I browse to http://www.example.com/node/10.html and I end up in http://www.example.com/node/10. In what way does it fail in your case?

And the one for "www" of course has been tested by thousands people with clean URLs. Have you added any other rewrites which may interfere?

The order is important. Also beware of the MultiViews option if it is enabled, because it can cause very unexpected results.

Interzone’s picture

Yep, it was the order, I was putting them at the bottom under the other rewrite rules. When I put them directly under the "RewriteEngine on" command, they work like a charm.

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

Thanks!

PhillReynolds’s picture

I was battling for days to get this to work until I read your post!
Had all the right stuff - but in the wrong place!
Moved it to directly after the "engine on" line and Robert's your father's brother! ( Bob's your Uncle! ).

Thanks a million :)
http://www.freemasonrycape.net

deepakjhalani.cms’s picture

Drupal can rewrites automatically dynamic url

by "clean URLs". (http://myurl.com/node/2)

Just enabaled Clean URLs under Site Configuration in drupal admin panel

otherwise if you can write these following lines in your .htaccess file

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