Forum Moderators: phranque

Message Too Old, No Replies

canonical problems

htaccess rewrite

         

pp46

5:19 pm on Dec 24, 2010 (gmt 0)

10+ Year Member



Hi

Google is listing my sites with and without www
To resolve this I have put an htaccess file at the root
with the following rule, replacing example with my domain and TLD

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


It works but I need to be sure 100% that its ok and will be read ok by the bots

For example if my index page is called index.php
should I not replace the 2 occurences of html in the rule with php
or does it not matter..?

I am far from being an expert on this I went on the apache web site and tied their example But could not get that to work probably missing something.

Thanks for any answers

g1smd

6:33 pm on Dec 24, 2010 (gmt 0)

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



Non-www index requests will see an unwanted double redirect.

Change the order of the rules, so that first rule redirects only index requests, and second rule redirects all OTHER requests.

Change
\.html
to
\.(php|html?)
to cater for .html, .htm and .php requests.

pp46

6:55 pm on Dec 24, 2010 (gmt 0)

10+ Year Member




Thanks for your fast reply

like this?

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.(php|html?)\ HTTP/
RewriteRule index\.html$ http://www.example.com/%1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

pp46

7:18 pm on Dec 24, 2010 (gmt 0)

10+ Year Member



I went and tried it like so
It does the job

pp46

7:20 pm on Dec 24, 2010 (gmt 0)

10+ Year Member



Thank you so much

:-)

g1smd

8:32 pm on Dec 24, 2010 (gmt 0)

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



You only got one of the .html fixed - there is one other place to change it.

pp46

8:49 pm on Dec 24, 2010 (gmt 0)

10+ Year Member



Thank you

like this ?
I had a doubt about the $

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.(php|html?)\ HTTP/
RewriteRule index\.(php|html?)$ http://www.example.com/%1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

g1smd

9:11 pm on Dec 24, 2010 (gmt 0)

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



That's the one.

You'd have found the code otherwise unresponsive to all index.php requests.

pp46

9:19 pm on Dec 24, 2010 (gmt 0)

10+ Year Member



Great one!
Thank you ever so much
cheers and merry xmas!

pp46

5:33 pm on Feb 8, 2011 (gmt 0)

10+ Year Member



HI
I am coming back here because I realised that when I have a site stucture with
the index page at the root and the rest of the site in a directory
like
index.php
site/index.php
site/page1.php.
site/page2.php
etc etc...


site/index.php
should redirect to
[site...]

The rewrite will of course not work
I have tried various things but they do not work
Can you point me in the right direction pls

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.(php|html?)\ HTTP/
RewriteRule index\.(php|html?)$ [mysite.fr...] [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.site/mysite\.fr$
RewriteRule ^(.*)$ [mysite.fr...] [R=301,L]

g1smd

5:51 pm on Feb 8, 2011 (gmt 0)

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



Use example.com to stop the forum auto-linking your example URLs.

To redirect single index URL in folder to root:

RewriteRule ^folder/index\.(php|html?) http://www.example.com/ [R=301,L]


This code goes ahead of other redirects as it is the most specific of all the rules.

pp46

6:36 pm on Feb 8, 2011 (gmt 0)

10+ Year Member



HI thanks for your answer!
not sure that I made myself understood?

for example I have an oscommerce site
the home page is at the root
http://www.example.com/index.php
the oscommerce site is in a dir called
http://www.example.com/site/
if somebody types in site/somepage.php
I want it to redirect to
http://www.example.com/site/somepage.php

Is that what you understood?

if so I modified the rule to look like this?!?

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.(php|html?)\ HTTP/
RewriteRule ^folder/index\.(php|html?) http://www.example.com/ [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.site/example\.fr$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]


Is that right?

pp46

2:56 pm on Feb 20, 2011 (gmt 0)

10+ Year Member



HI

I have been trying to figure this out and have not succeeded.

Maybe its because I already have url rewriting set up
with this
 RewriteBase /folder/
RewriteRule ^(.*)-p-([0-9]+).html$ product_info.php?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-c-([0-9_]+).html$ index.php?cPath=$2&%{QUERY_STRING}
RewriteRule ^(.*)-m-([0-9]+).html$ index.php?manufacturers_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pi-([0-9]+).html$ popup_image.php?pID=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pr-([0-9]+).html$ product_reviews.php?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pri-([0-9]+).html$ product_reviews_info.php?products_id=$2&%{QUERY_STRING}



And when I add this it just redirects to www.example.com

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.(php|html?)\ HTTP/
RewriteRule ^folder/index\.(php|html?) http://www.example.com/ [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.site/example\.fr$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]



Thanks for any ideas pls

g1smd

1:33 am on Feb 22, 2011 (gmt 0)

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



The redirects must be listed before the rewrites.

Every rule should have the [L] flag on the end.

The (.*) pattern is very non-optimum. The (.*) pattern requires hundreds of back off and retry trial matches. The (.*) pattern should be changed for something that can be parsed left to right in one cycle.