Forum Moderators: phranque

Message Too Old, No Replies

.htaccess rewrite

from file without extension to one with an extension

         

IanTurner

4:12 pm on Apr 13, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I need to be able to to do a 301 rewrite from

http://www.example.com/youarehere

to

http://www.example.com/youarehere.html

however everything that I have tried so far just gets into a rewrite loop.

g1smd

7:56 pm on Apr 13, 2011 (gmt 0)

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



An internal rewrite does not produce a new URL, it maps an incoming URL request to an internal server filepath and file.

Publish links pointing at the extensionless URL and use a rewrite to map those requests to the right file:

RewriteRule ^(([^/]+/)*[^/.]+)$ /$1.html [L]


An external 301 redirect tells the browser to make a new request for a different URL. Use this to stop direct access to the files and force the user to use the extensionless URL:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^/.]+\.html\ HTTP/
RewriteRule ^(([^/]+/)*[^/.]+)\.html$ http://www.example.com/$1 [R=301,L]


There's no such thing as a "301 rewrite". It's either an "internal rewrite" or an "external 301 redirect".

IanTurner

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

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Was thinking about rewrites when I was posting thats why I typed 301 rewrite (you are correct that it should be a 301 redirect.)

Someone originally linked to the file without the extension and both the extensionless link and the link with the extension are normally sent to the same file by the apache server.

What I want to do is get rid of all the links to the extensionless file and 301 redirect them to the URL using the extension.

g1smd

9:31 pm on Apr 13, 2011 (gmt 0)

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



The code above does the exact opposite of what you want. It redirects .html URL requests to the alternative extensionless URL. It accepts extensionless URL requests and rewrites them to the internal .html file.

Why do you want the reverse?

IanTurner

8:10 am on Apr 14, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I want to do the reverse because I have a page that has been known and linked to by both.

Effectively at the moment it is like two pages with the same content exist.

The URL without the extension has about 20% of the links of the URL with the extension - so the sensible thing to do is to 301 redirect the URL with the fewest links.

g1smd

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

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



But is it?

If your site has taken the leap to go "extensionless" I would stick to that and redirect requests for .html URLs.

Most of the link benefit will be transferred to the extensionless URL by the redirect.

IanTurner

11:06 am on Apr 14, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The site hasn't taken the leap to go extensionless - this is a single page that was out of step with the rest of the site.

jdMorgan

3:06 pm on Apr 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteRule ^single-extensionless-page$ http://www.example.com/single-extensionless-page.html [R=301,L]

If this loops, then that means another rule is interfering, and you'll need to use an appropriate version of the "THE_REQUEST" method above to prevent that. But try this trivial solution first.

Jim

g1smd

8:01 pm on Apr 14, 2011 (gmt 0)

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



Ah, noted that that this is just one page, I had incorrectly assumed that it was not.

In that case, yes, redirect to the .html URL. :)