Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewriting around subdomains

How to mod rewrite and not redirect subdomains

         

CainIV

6:49 pm on Oct 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi guys. I am trying to use mod rewrite to write non www urls to www urls in my site, as well as all subdomains.

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

jdMorgan

7:29 pm on Oct 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to add a missing "www" to any and all hostnames, the following should work:

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]

Note that the first rule has no "L" flag, in case an "index.php" redirect is also needed. Note also that this rule has been modified to redirect only direct client requests for "index.php" to "/" -- Otherwise, you may find it impossible to use a file named "index.php" as your DirectoryIndex file without invoking an 'infinite' rewrite loop if this code is in .htaccess.

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]

CainIV

4:30 am on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteEngine on
#
# Add "www" if missing from requested hostname
RewriteCond %{HTTP_HOST}!^www\.
RewriteRule (.*) [%{HTTP_HOST}...] [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_HOST}...] [R=301,L]

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?

jdMorgan

5:00 am on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you have only one valid/preferred subdomain, then the simplest solution is just to brute-force it:

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]

Jim

[edited by: jdMorgan at 5:00 am (utc) on Oct. 19, 2006]

CainIV

5:50 am on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thankyou very much. JD might I ask, what does the exclamation do before:

RewriteCond %{HTTP_HOST}!^www\.subdomain\.example\.com

I understand the rest of the operators.

Todd

CainIV

5:52 am on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think I understand, it's an 'is not equal to' operator, right?