Forum Moderators: phranque

Message Too Old, No Replies

rewrite rules: bug

         

specter

7:02 pm on Aug 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

I set up some redirs for my domain and sub (canonicalization):

That's the set:

# Redirect all non-canonical domain requests to requested resource
# in canonical domain except for recognized subdomains
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteCond %{HTTP_HOST}!^sub\.example\.com

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

Discovered that Google still indexes subdomain pages as sub.domain.com and domain.com/sub/. as my sub still is available in both formats.

I temporarily fixed this by preceding that rules by the following redir command:

redirect permanent /sub [sub.example.com...]

But someone told me that is not the best way:
I should add a rewrite rule I guess...
I tried this one by placing it before the last rule above, but it doesn't work...

RewriteRule (.*) [sub.example.com...] [R=301,R]

Can someone enlight me,please?
I'm not very friendly with mod-rewrite...

Thnks

jdMorgan

3:15 am on Aug 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is not clear how you are currently rewriting the subdomain requests to the subdirectory, but I assume you already have an internal rewrite to do that.

In order to prevent that internal rewrite and this new external redirect that you wish to add from interfering with each other to create an 'infinite' rewrite/redirect loop, you must check that the request for the subdomain's subdirectory is a direct request from the client (browser) and not the result of your original subdomain-to-subdirectory internal rewrite. This checking can be done using THE_REQUEST which holds the original client HTTP request header:


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

Jim

specter

9:49 am on Aug 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it works fine.

So...

I tried my best to integrate that rule with the existing ones but unsuccessfully.
301 Rules seem to conflict.

Please,help me out...

jdMorgan

3:55 pm on Aug 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can we see the entire set of redirects and rewrites that you tested, please?

Jim

specter

6:40 pm on Aug 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well,

In order to test I used only your directives, like this:

# -FrontPage-

IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*

Order deny,allow
<Limit GET POST>
Allow from all
</Limit>

<Limit PUT DELETE>
Deny from all
</Limit>

AuthName www.example.net
AuthUserFile /home/user/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/user/public_html/_vti_pvt/service.grp
AddType application/x-httpd-cgi .htm

Options -Indexes +FollowSymLinks
RewriteEngine on

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sub/

RewriteRule ^sub/(.*)$ [sub.example.net...] [R=301,L]

as you suggested.

jdMorgan

6:50 pm on Aug 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I tried my best to integrate that rule with the existing ones but unsuccessfully.
301 Rules seem to conflict.

What other rules, please? I cannot diagnose what is wrong with your horse if I cannot see both ends... :)

Jim

specter

7:21 pm on Aug 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok.The whole rules set; your rule in bolded text:

# -FrontPage-

IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*

Order deny,allow
<Limit GET POST>
Allow from all
</Limit>

<Limit PUT DELETE>
Deny from all
</Limit>

AuthName www.example.net
AuthUserFile /home/user/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/user/public_html/_vti_pvt/service.grp
AddType application/x-httpd-cgi .htm

Options -Indexes +FollowSymLinks
RewriteEngine on

# Redirect all non-canonical domain requests to requested resource in canonical domain
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.example\.net
RewriteCond %{HTTP_HOST}!^sub\.example\.net

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sub/

RewriteRule ^sub/(.*)$ [sub.example.net...] [R=301]

RewriteRule (.*) http://www.example.net/$1 [R=301,L]

jdMorgan

5:10 am on Aug 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, it looks like you inserted the new rule into the middle of your existing rule, and that would be, er, bad... :)

This is what the mod_rewrite portion of your file should llok like:


Options -Indexes +FollowSymLinks
RewriteEngine on
#
# Redirect direct client requests for subdomain-subdirectory to root dir of subdomain
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sub/
RewriteRule ^sub/(.*)$ http://sub.example.net/$1 [R=301]
#
# Redirect all non-canonical domain requests to requested resource in canonical domain
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.net
RewriteCond %{HTTP_HOST} !^sub\.example\.net
RewriteRule (.*) http://www.example.net/$1 [R=301,L]

Jim

specter

8:45 am on Aug 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



..Oh,well!

So the key was to compose two separate sets of rules...

of course now it works fine.

Thanks very much again jim.

You are a saint! :)