Forum Moderators: phranque

Message Too Old, No Replies

Rewrite ".html" to "" to ".html" = loop (of course). So.

         

Roan

10:34 am on Sep 16, 2010 (gmt 0)

10+ Year Member



Hi

I've a static site which is currently in html with .html suffix.

My problem is that the site has already been extensively indexed by search engines which send the full request [website...]

What I want to do is still allow the request to get hold of the html file... of course.... but to display a completely clean url without the file type at all ( and therefore encourage search engines to re-index with the cleaner url )

I've played around with .htaccess for days over several weeks and just keep getting infinite loops.

Is this as simple as I think it should be?

Thanks for taking the time.

g1smd

6:22 pm on Sep 16, 2010 (gmt 0)

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



Yes, it is simple. There's three steps involved.

Set up a 301 redirect from /whatever.html to www.example.com/whatever noting that this redirect should fire only for direct client URL requests. Use a preceding RewriteCond to test THE_REQUEST.

Set up a rewrite that accepts URL requests for example.com/whatever and rewrite the request to the internal filepath /whatever.html.

On the pages of the site, link only to the clean "/whatever" or www.example.com/whatever URL.

Use RewriteRule for both of the above rules. Do NOT use Redirect or RedirectMatch.

jdMorgan

4:05 pm on Sep 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See this thread on implementing extensionless URLs from our Apache Forum Library: [webmasterworld.com...]

Jim

Roan

11:15 pm on Oct 3, 2010 (gmt 0)

10+ Year Member



Hi Guys. I've not forgotten this... just away chasing my tail. I appreciate your advice and I'll get round to it. Thanks.

t_penible

8:26 am on Oct 4, 2010 (gmt 0)

10+ Year Member



Hi Guys !

I think I've the same problem with a 301 redirection and friendly URL rewriting. I've tried to follow the hints given by g1smd, but I can't manage to make this work.

I have this for the moment in my .htaccess :

Options +FollowSymlinks
RewriteEngine On

#301 - Contact
RewriteCond %{REQUEST_URI} ^/dir/subdir/nl_NL/contact\.php$
RewriteRule ^contact.php$ http://samedomain.com/dir/subdir/nl/contact/ [R=301]

RewriteRule ^contact/$ contact.php [L,NC,QSA]


But it does a loop with the error : The page is not redirecting properly.

I just want to make friendly URL like
http://www.domain.com/nl/contact/
instead of
http://www.domain.com/nl_NL/contact.php
and saving the page rank with a 301.

Can you help me please ?
Thanks you and sorry for my poor english.

sublime1

1:30 pm on Oct 4, 2010 (gmt 0)

10+ Year Member



Roan and t_penible --

You might also take a look at this thread from earlier this week -- I fell into the same trap (in a failed attempt to answer) and was schooled by Jim and g1smd. [webmasterworld.com...]

Tom

t_penible

1:53 pm on Oct 4, 2010 (gmt 0)

10+ Year Member



Thanks, I've found the solution just now !

In fact, I was testing the REQUEST_URI instead of the THE_REQUEST. By following the steps I have something like that :

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*contact\.php\ HTTP/ 
RewriteRule ^contact\.php$ http://exchange.brytebarcelona.com/zonneman/tienda/nl/contact/ [R=301,L]

RewriteRule ^contact/$ contact.php [L,NC,QSA]


And It works !

Thanks.

jdMorgan

2:56 pm on Oct 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The ".*" subpattern preceding "contact\.php" in your RewriteCond is both inefficient and, in this case, unnecessary.

Be aware that if it is possible that "/contact.php" will be requested with a query string or URI-fragment appended, the rule will fail. You might consider allowing for it in case such a URL is mistakenly or maliciously requested:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /contact\.php([?#][^\ ]*)?\ HTTP/
RewriteRule ^contact\.php$ http://exchange.brytebarcelona.com/zonneman/tienda/nl/contact/? [R=301,L]
#
RewriteRule ^contact/$ contact.php [NC,L]

As shown, the code will allow for (and remove) any query strings or URI-fragments.

The use of [QSA] in the second rule is also unnecessary, since you are not appending any additional query string name/value pairs.

Further, since you are rewriting these requests to your script without any upper/lowercase requirements, your script must check for proper casing, and redirect any mis-cased requests to the correctly-cased URL. Otherwise, your risk duplicate-content problems.

Jim