Forum Moderators: phranque
RewriteEngine On
RewriteRule ^index.php/$ [mysite.com...] [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite\.com
RewriteRule (.*) [mysite.com...] [R=301,L]
I have yet to find any logical way to prevent my subdomains from redirecting to the root www.mysite.com.
Ideally, all subdomains would instead redirect from their non www to their www versions.
Any ideas would be appreciated.
Todd
RewriteEngine on
#
# Add "www" if missing from requested hostname
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301]
#
# Rewrite direct client requests for /index.php to "/" in same (sub)domain
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php[?/]?
RewriteRule ^index\.php/?$ http://%{HTTP_HOST}/ [R=301,L]
The "www" code above has several holes in it. First, if a request for a doamin called "ww.example.com" is received, the rule will redirect that to www.ww.example.com. Or if a request for subdomain.www.example.com is received, that will be redirected to www.subdomain.www.example.com. The only way to avoid that is to have the rule explicitly recognize all valid hostnames, and correct the invalid ones. This means the code would need to contain a pattern listing all valid hostnames.
Jim
[edited by: jdMorgan at 7:30 pm (utc) on Oct. 18, 2006]
First, if a request for a doamin called "ww.example.com" is received, the rule will redirect that to www.ww.example.com. Or if a request for subdomain.www.example.com is received, that will be redirected to www.subdomain.www.example.com. The only way to avoid that is to have the rule explicitly recognize all valid hostnames, and correct the invalid ones. This means the code would need to contain a pattern listing all valid hostnames.
I appreciate the assistance JD. There is only one subdomain on this website, let's call it webcompany.
The following code I have tried but it has so far not worked to fix the holes you describe:
RewriteRule ^www.subdomain.www.example.com$ [%{HTTP_HOST}...] [R=301]
RewriteRule ^www.ww.example.com$ [%{HTTP_HOST}...] [R=301]
It seems as though I am missing finding an easier way to trap all versions of the subdomain and then screen invalid requests out?
RewriteEngine on
#
# If non-canonical hostname containing "subdomain" is requested,
# redirect to canonical www.subdomain.example.com
RewriteCond %{HTTP_HOST} subdomain
RewriteCond %{HTTP_HOST} !^www\.subdomain\.example\.com
RewriteRule (.*) http://www.subdomain.example.com [R=301]
#
# If non-canonical hostname NOT containing "subdomain" is
# requested, redirect to canonical www.example.com
RewriteCond %{HTTP_HOST} !subdomain
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com [R=301]
#
# Rewrite direct client requests for /index.php to "/"
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php[?/]?
RewriteRule ^index\.php/?$ http://%{HTTP_HOST}/ [R=301,L]
[edited by: jdMorgan at 5:00 am (utc) on Oct. 19, 2006]