subdomain to different url without changing browser

Using a single web hosting account to host multiple sites

subdomain to different url without changing browser

Postby jwilson » Wed Dec 10, 2008 9:22 am

I have a situation where I need a specific domain to be rewritten to a different URL while staying the same on the browser.

www.domain.com goes to index.php and it handles all the requests.

us.domain.com should go to index.php/us but still look like "us.domain.com" on the browser, without the "/us" on the end. From that point forward, the second subdomain should work exactly like the www one - no need to put the "/us" in front.

I tried this but it doesn't seem to do it:

# Redirect for US site
RewriteCond %{HTTP_HOST} ^us.domain.com$ [NC]
RewriteRule ^/$ index.php/us [L]

Any hints?
jwilson
 
Posts: 10
Joined: Wed Dec 10, 2008 9:12 am

Postby richardk » Thu Dec 11, 2008 7:43 am

Does the sub domain already work (does it go to the index.php file)?
What do you want to happen if they go to a file that exists, eg. us.example.com/style.css and example.com/style.css exists?
What is your full mod_rewrite?
Where are you putting the mod_rewrite?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^us\.example\.com$ [NC]
RewriteRule ^(.*)$ /index.php/us/$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jwilson » Thu Dec 11, 2008 8:09 am

richardk wrote:Does the sub domain already work (does it go to the index.php file)?


Yes. The first VirtualHost that serves the main site is the catch-all for the various subdomains. So us.domain.com/index.php brings up the main site.

What do you want to happen if they go to a file that exists, eg. us.example.com/style.css and example.com/style.css exists?


To go to the files without adding the /us on the front.

What is your full mod_rewrite?


There are a bunch of other ones to support legacy URLs or the french site. They are all after the US rule so shouldn't matter. For example:

# Legacy company access
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^m[0-9]+\.domain.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com
RewriteRule ^/(.*)$ http://www.domain.com/companies/$1 [R=301,L]

Where are you putting the mod_rewrite?


After this rule:

RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]
jwilson
 
Posts: 10
Joined: Wed Dec 10, 2008 9:12 am

Postby richardk » Thu Dec 11, 2008 8:35 am

Is it in the <VirtualHost>?
Do you not have some rule for example.com/something to /index.php/something?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^us\.example\.com$ [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(/.*)$ /index.php/us$1 [QSA,L]

# Legacy company access
RewriteCond %{HTTP_HOST} !^(www|m[0-9]+)\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule ^(/.*)$ http://www.example.com/companies$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jwilson » Thu Dec 11, 2008 8:55 am

richardk wrote:Is it in the <VirtualHost>?
Do you not have some rule for example.com/something to /index.php/something?


Ahh yes, the very last rule.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

All these rules are within the VirtualHost.
jwilson
 
Posts: 10
Joined: Wed Dec 10, 2008 9:12 am

Postby jwilson » Thu Dec 11, 2008 9:03 am

Talking to my boss, it turns out he only wants the initial call to us.domain.com to go to index.php/us. Everything after that should just go to the regular index.php.

So maybe I should just check to see if the PATH_INFO is empty?
jwilson
 
Posts: 10
Joined: Wed Dec 10, 2008 9:12 am

Postby richardk » Thu Dec 11, 2008 11:30 am

The following should work
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^us\.example\.com$ [NC]
RewriteRule ^/$ /index.php/us [QSA,L]

# Legacy company access
RewriteCond %{HTTP_HOST} !^(www|m[0-9]+|us)\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule ^(/.*)$ http://www.example.com/companies$1 [R=301,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(/.*)$ /index.php$1 [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jwilson » Thu Dec 11, 2008 11:50 am

Nope, it seems to just pass through into the regular index.php. Actually, the revamped last rule caused an infinite loop of reloads until I moved the slash from the rule to the result.

Here's my complete virtualhost for reference:

<VirtualHost 192.168.1.1>
Servername www.domain.com
DocumentRoot /virtual/domain

ErrorLog /var/log/httpd/error.log
CustomLog /var/log/httpd/access.log combined
ErrorDocument 500 /500.html
ErrorDocument 404 /404.html
LogLevel warn

Alias /content /virtual/images
Alias /georef /virtual/georef

<Directory "/virtual/domain">
Options +FollowSymLinks
AllowOverride None
RewriteEngine On

# Prefer www.domain.com to domain.com
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]

# Redirect for US site
RewriteCond %{HTTP_HOST} ^us\.domain\.com$ [NC]
RewriteRule ^/$ /index.php/us [QSA,L]

# Captures wierd legacy domain names
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]

# Legacy company access
RewriteCond %{HTTP_HOST} !^(www|m[0-9]+)\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteRule ^(/.*)$ http://www.domain.com/companies$1 [R=301,L]

# Needed for embeddings
RewriteRule ^(.*).iframe$ http://www.domain.com/embed/$1 [L]

# Route french searches to the french site
RewriteRule ^/trouvez/(.*)$ http://www.domain.com/fr/trouvez/$1 [R=301,L]

# Redirect /fr/find/ to /fr/trouvez
RewriteRule ^/fr/find/(.*)$ http://www.domain.com/fr/trouvez/$1 [R=301,L]

# Redirect /find/XXXXXX/ to /find/XXXXXX
RewriteRule ^/find/(.*)/$ http://www.domain.com/find/$1 [R=301,L]

# Redirect http://www.domain.com/fr to http://www.domain.com/fr/
RewriteRule ^/fr$ http://www.domain.com/fr/ [R=301,L]

# Redirct http://www.domain.com/?source=olddomain to http://www.domain.com/
RewriteCond %{query_string} &?source=olddomain.*
RewriteRule ^/$ http://www.domain.com/ [R=301,L]

# Redirect http://www.domain.com/?lang=fr and http://www.domain.com/?language=fr to http://www.domain.com/fr/
RewriteCond %{query_string} &?lang=fr.* [OR]
RewriteCond %{query_string} &?language=fr.*
RewriteRule ^/$ http://www.domain.com/fr/ [R=301,L]

# Redirect http://www.domain.com/index.php?lang=fr and http://www.domain.com/index.php?language=fr to http://www.domain.com/fr/
RewriteCond %{query_string} &?lang=fr.* [OR]
RewriteCond %{query_string} &?language=fr.*
RewriteRule ^/index.php$ http://www.domain.com/fr/ [R=301,L]

# Redirect http://www.domain.com/index.php?lang=en and http://www.domain.com/index.php?language=en to http://www.domain.com/
RewriteCond %{query_string} &?lang=en.* [OR]
RewriteCond %{query_string} &?language=en.*
RewriteRule ^/index.php$ http://www.domain.com/ [R=301,L]

# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</directory>

# Compress all data out, except images
<Location />
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
</Location>
</VirtualHost>

Thanks for the help, by the way. Why this doesn't work is driving me crazy.
jwilson
 
Posts: 10
Joined: Wed Dec 10, 2008 9:12 am

Postby richardk » Sat Dec 13, 2008 9:08 am

Does the /trouvez/ redirect work? (Before and after the new mod_rewrite).
The mod_rewrite is actually inside a <Directory>.

Try
Code: Select all
<VirtualHost 192.168.1.1>
  Servername www.domain.com
  ServerAlias  *.domain.com
  DocumentRoot /virtual/domain

  ErrorLog /var/log/httpd/error.log
  CustomLog /var/log/httpd/access.log combined
  ErrorDocument 500 /500.html
  ErrorDocument 404 /404.html
  LogLevel warn

  Alias /content /virtual/images
  Alias /georef /virtual/georef

  <Directory "/virtual/domain">
    Options +FollowSymLinks
    AllowOverride None
  </Directory>

  # Compress all data out, except images
  <Location />
    SetOutputFilter DEFLATE
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    Header append Vary User-Agent env=!dont-vary
  </Location>

  RewriteEngine On

  # Captures wierd legacy domain names
  RewriteCond %{HTTP_HOST} ^www\.
  RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
  RewriteRule ^(/.*)$ http://www.domain.com$1 [R=301,L]

  # Legacy company access
  RewriteCond %{HTTP_HOST} !^(www|m[0-9]+|us)\.domain\.com$ [NC]
  RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
  RewriteRule ^(/.*)$ http://www.domain.com/companies$1 [R=301,L]

  # Prefer www.domain.com to domain.com
  RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
  RewriteRule ^(/.*)$ http://www.domain.com$1 [R=301,L]

  # Redirect for US site
  RewriteCond %{HTTP_HOST} ^us\.domain\.com$ [NC]
  RewriteRule ^/$ /index.php/us [QSA,L]

  # Needed for embeddings
  RewriteRule ^(/.+)\.iframe$ http://www.domain.com/embed$1 [L]

  # Route french searches to the french site
  RewriteRule ^/trouvez/(.*)$ http://www.domain.com/fr/trouvez/$1 [R=301,L]

  # Redirect /fr/find/ to /fr/trouvez
  RewriteRule ^/fr/find/(.*)$ http://www.domain.com/fr/trouvez/$1 [R=301,L]

  # Redirect /find/XXXXXX/ to /find/XXXXXX
  RewriteRule ^/find/(.*)/$ http://www.domain.com/find/$1 [R=301,L]

  # Redirect http://www.domain.com/fr to http://www.domain.com/fr/
  RewriteRule ^/fr$ http://www.domain.com/fr/ [R=301,L]

  # Redirct http://www.domain.com/?source=olddomain to http://www.domain.com/
  RewriteCond %{QUERY_STRING} ^(.*&)?source=olddomain(&.*)?$ [NC]
  RewriteRule ^/$ http://www.domain.com/ [R=301,L]

  # Redirect http://www.domain.com/?lang=fr and http://www.domain.com/?language=fr to http://www.domain.com/fr/
  # Redirect http://www.domain.com/index.php?lang=fr and http://www.domain.com/index.php?language=fr to http://www.domain.com/fr/
  RewriteCond %{query_string} ^(.*&)?lang(uage)?=fr(&.*)?$ [NC]
  RewriteRule ^/(index\.php)?$ http://www.domain.com/fr/ [R=301,L]

  # Redirect http://www.domain.com/index.php?lang=en and http://www.domain.com/index.php?language=en to http://www.domain.com/
  RewriteCond %{query_string} &?lang=en.* [OR]
  RewriteCond %{query_string} &?language=en.*
  RewriteRule ^/index.php$ http://www.domain.com/ [R=301,L]

  # Checks to see if the user is attempting to access a valid file,
  # such as an image or css document, if this isn't true it sends the
  # request to index.php
  RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
  RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
  RewriteRule ^(/.*)$ /index.php$1 [L]
</VirtualHost>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jwilson » Mon Dec 15, 2008 6:32 am

I tried putting those rules in place and it caused the site to 404.

Removing the leading /'s from the main redirect allowed the site to work, but then every link would cause a BAD REQUEST error.
jwilson
 
Posts: 10
Joined: Wed Dec 10, 2008 9:12 am

Next

Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 23 guests

cron