Forum Moderators: phranque

Message Too Old, No Replies

Redirect all .html requests

remove the .html part from web requests

         

tmccar

8:45 am on Apr 26, 2011 (gmt 0)

10+ Year Member



I have been experimenting with various permalink settings on my website, with the result that I now see many "not found" error is Google Webmasters. These refer to a webpage with a .html extension - if the .html is removed, the page loads fine.
How can I do a redirect in .htaccess for all such requests (i.e. "strip off" the .html part of the link)

Thanks
Tom

g1smd

8:59 am on Apr 26, 2011 (gmt 0)

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



This code, or something similar, has been posted several times in the last few weeks:

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


If you also have an internal rewrite, one that maps incoming extensionless URL requests to real .html files inside the server, you will need to add a RewriteCond before the Rule. This Condition will test THE_REQUEST and ensure that only direct client requests for .html are redirected. Without the extra Condition, internally rewritten requests will rematch the rule and form an infinite internal rewrite loop.

Be aware the rule above redirects ALL .html requests. You may need to also add a negative match RewriteCond exclusion for certain file requests that should not be rewritten; such as requests for your WMT or gA verification file and so on.

tmccar

9:47 am on Apr 26, 2011 (gmt 0)

10+ Year Member



Sorry but that has not made any difference. Here is an example of such a link:
[scientificknowledge.org...]

g1smd

8:34 pm on Apr 26, 2011 (gmt 0)

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



I assume you have the usual

Options +FollowSymLinks
RewriteEngine On


prefixed before the other code.

I also assume that you have turned off MultiViews and so on.

tmccar

11:33 am on Apr 27, 2011 (gmt 0)

10+ Year Member



Yes, sir:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress


Options +FollowSymlinks
Options +Indexes


RewriteRule ^(([^/]+/)*[^/.]+)\.html$ [scientificknowledge.org...] [R=301,L]



Multiviews?

g1smd

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

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



From the code above the error is one that is often made.

After the internal rewrite code has rewritten the request to fetch the content from the server hard drive, and just as the magnetic head is about to read the data off the disk, along comes an instruction to tell the user to make a new request for a different URL.

Since the internal pointer contains the long unfriendly path generated after the rewrite code has run, this redirect to a new URL will contain the value of that pointer and thereby exposes the rewritten long filepath back out on to the web as a new URL.

As a first step, place the redirect code before the internal rewrite code. The redirect code may also need to test THE_REQUEST to ensure the request is from an external source.

tmccar

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

10+ Year Member



I did that , and it worked! Thanks
Tom