Forum Moderators: phranque
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
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]
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.
# -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]
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]