Forum Moderators: phranque
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!
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
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!
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]
Jim
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!
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