Forum Moderators: phranque

Message Too Old, No Replies

Setting up Internal Redirection for Subdomains

Basic question, but very specific.

         

Ertyuyu

8:56 pm on May 3, 2009 (gmt 0)

10+ Year Member



I want to be able to use a sub-domain to redirect to a different part of a website, without causing the address visible in the client's browser to change.

My webhosting service (via cPanel) gives me the option of creating a subdomain, then redirecting. The problem, if I understand this properly, is that this creates an external redirect, which re-writes the address in the browser URL.

Here's the code from the .htaccess file in the subdomain directory, automatically generated by cPanel:

==========

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example1.example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example1.example.com$
RewriteRule ^.*$ "http\:\/\/www\.example\.com\/index\.php\/site\/example1\/" [R=302,L]

==========

My questions are:

1. Do I understand correctly that to do an internal redirect, I want to modify the .htaccess file for the primary domain, and not the subdomain?

2. What is it about the syntax above that specifies this is an external redirect, and how do I change it to an internal redirect?

[edited by: engine at 7:24 am (utc) on May 7, 2009]
[edit reason] examplified [/edit]

jdMorgan

2:02 pm on May 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1. The answer depends on how your server is configured to "map" HTTP requests for your new subdomain into the filespace of your account. The code you posted abovee implies that the subdomain is mapped to the root directory of your "main" Web site's filespace, and that the only difference in handling the main domain and the new subdomain is that "index.php/site/example1/" is substituted by this redirect for any URL-path (often improperly called a "filename") originally requested by the visitor.

The effects of this are that the path requested by the visitor will be lost, and the example.com/site/example1/ will appear in the visitor's browser address bar.

2. For servers where all subdomains (including the www subdomain of example.com) are mapped to the root directory, the following root-level .htaccess code is typical. This is an internal rewrite, not a redirect. However, since I do not know how your server is configured or how you wish to handle your new subdomain, I do not claim that this will work for you.


# If the request is not for the the main domain or the www subdomain
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com
# get the requested subdomain name into variable %2
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
# and if not already rewritten to subdomain's subdirectory
RewriteCond $1 !^site/
# internally rewrite to prefix "/site/<subdomain-name>/" to the client-requested URL-path
RewriteRule ^(.*)$ /site/%2/$1 [L]

Note that the RewriteConds in this code can be simplified if you already have a redirect in place to force canonicalization of the non-www main hostname to the www hostname (as you should) and to removw "www." from all other subdomain requests.

Jim