Forum Moderators: phranque

Message Too Old, No Replies

Redirecting domain names

redirecting domain names

         

erikcw

4:51 am on Mar 2, 2004 (gmt 0)

10+ Year Member



Hey all,

I'm a bit of a newbie on this issue, but I feel that mod-rewrite may be the solution to my problem.

I have several websites on a virtual hosting package, all supporting one main site. The primary site's files are located at / and the other sites are located in /sites/domain-a/... (I have all of the files for all sites hosted under the main domain, and each sub-site redirects to the subdirectory of the main site containing the files for that site)

What I want to do is use .htaccess and mod-rewrite for each domain and have it rewrite the URL for the sub site to the actual location.
Example:
/(.*)$ /sites/domain-a/(.*)$

How do I do this in mod-rewrite - AND is this the best approach to setting this up?

Thanks!

jdMorgan

5:16 am on Mar 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



erikcw,

Welcome to WebmasterWorld [webmasterworld.com]!

Try this search [google.com] to get started.

As to whether it's the best approach, that's a matter of personal opinion. I've used the method for several years, and I'm satified with it.

Jim

erikcw

4:02 am on Mar 3, 2004 (gmt 0)

10+ Year Member



I got the redirect portion to work - but I want to mask the URL.
Righat now domain-a.com/test/ redirects to domain-b.com/test/

I want domain-b.com's files, but domain-a.com in the address bar. I placed the following in domain-a.com's htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} domain-a.com$
RewriteCond %{REQUEST_URI}!^/sites/domain-a.com/
RewriteRule (.*) /sites/domain-a.com/$1

It still just does a redirect. What am I missing?

Thanks!

jdMorgan

4:32 am on Mar 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code you posted does not do an external redirect, and so should not change the browser address bar. However, some other RewriteRule, or a Redirect or RedirectMatch directive may be doing that.

To avoid causing this problem with RewriteRules, Make sure you put an [L] flag on all of your rules, unless you have a good reason not to.

Your code looks pretty good; Here it is with some minor cleanups. Note that literal periods in regex patterns need to be escaped by preceding them with "\" and you should basically never end-anchor a hostname or add a slash to it, because the code will break if a port number is appended - i.e. www.domain-a.com:80/


RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain-a\.com
RewriteCond %{REQUEST_URI} !^/sites/domain-a\.com/
RewriteRule (.*) /sites/domain-a.com/$1 [L]

Look around for any other directives that may be interfering and causing an external redirect. This also could be caused by code in higher-level .htaccess or httpd.conf files, or in lower-level .htaccess files as well.\

Jim

erikcw

5:08 am on Mar 3, 2004 (gmt 0)

10+ Year Member



I updated my code with your cleaner version, but I am still not getting any results. Here are my .htaccess files from each domain.

domain-a.com - .htaccess (domain to be redirected)

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} domain-a.com$
RewriteRule ^(.*)$ [domain-b.com...] [r]

domain-b.com - .htaccess (host with domain-a.com's content)

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule article\/(.*)\/$ /article\.php?id=$1
RewriteRule article\/(.*)$ /article\.php?id=$1
RewriteCond %{HTTP_HOST} ^(www\.)?domain-a\.com
RewriteCond %{REQUEST_URI}!^/sites/domain-a\.com/
RewriteRule (.*) /sites/domain-a.com/$1 [L]
Redirect 302 /index.php [domain-b.com...]

It is my understanding that this code would:
1) Direct domain-a.com => domain-b.com/sites/domain-a.com/
2) Show domain-a.com in the address bar instead of domain-b.com/sites/domain-a.com (so that visitors will never see the long ugly URL).

Thanks for your help!

jdMorgan

5:26 am on Mar 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you use an [r] flag and a canonical URL in the substitution (e.g. http://www.domain...). then that is an external redirect by definition, and the redirect response is sent to the client browser, which then issues a whole new HTTP request with the address you just gave it, thus updating the browser address bar.

In contrast, the code I posted does not use an [R] flag, and does not specify a canonical URL, but rather uses a local path instead. This is, in effect, a simple local file substitution. Now, obviously, this only works if the two "domains" actually reside in the same "account" at or below the directory where the .htaccess code is installed. If not, then you are going to need to set up a proxy connection so that content for domain-a is "pulled over" transparently from domain-b, with domain-a acting as a proxy server for domain-a/sites/domain-b content.

Bear in mind that almost every single character in a piece of mod_rewrite code is significant, and that one little regex operator or mod_rewrite flag can completely change the results. Thus, there is some latitude for different "styles", but not much.

Jim