mod_rewrite to redirect host to internal directory

I’m trying to use mod_rewrite (.htaccess files) to deal with multiple domain names on my shared (virtual) web hosting account. So I don’t have console access to the server, nor can I change the httpd.conf file.

Currently I have several domain names pointing to my account on the server, but they all access files in the directory tree /home/myusername/public_html (i.e. they all show the same content). I would like to redirect some of the domains to other directory trees, so for example:
http://mydomain1.com accesses files in /home/myusername/dom1/
http://mydomain2.com accesses files in /home/myusername/dom2/

This would, it seems to me, allow each domain name to show different content.

I would like to use internal redirects to do this, so the user is unaware that he is not accessing a unique server for each domain name. This would of course need to work for other files referenced my links in the web pages, such as css and image files.

How do I do this in my .htaccess file?

Thanks - Rowan

Hi Rowan!

First, WELCOME to SitePoint’s Apache forum.

Second,
[rant #3][indent]My purpose for being here is to share knowledge, not to do free coding. I consider requests for free code to be from “script kiddies” and will not respond to them. Take the time and make an effort to learn and you’ll get all the assistance you need![/indent][/rant #3]

If you want to learn mod_rewrite, please start with the tutorial linked in my signature. When you show the code you believe will work for you, I’ll be happy to help guide you through correcting and optimizing for you.

Regards,

DK

Sorry for not following the protocol! I’ve now read your tutorial, but it didn’t seem to help, principally because it didn’t seem to have a section on the RewriteRule statement which is what I am having trouble understanding.

For an internal redirect (which, if I’ve understood this correctly, is what happens if I do NOT specify an R flag), is the second parameter

  • an absolute (if it starts with /) or relative (if it doesn’t) physical path on the server?
  • If relative, relative to what? Or does the presence of a leading / make no difference?
  • Or an absolute or relative path from the document root of my web site (in my case this is /home/myaccount/public_html)
  • If relative, relative to what? Or does the presence of a leading / make no difference?

And what exactly does RewriteBase do? If I’ve understood it correctly, it doesn’t seem to help with what I’m trying to do.

Is it actually possible to do an internal redirect to a directory that’s not a subdirectory of the document root of my web site? I.e. to any directory on my server? I’ve seen some examples that seem to suggest it is, and others that suggest it’s not.

Here’s part of my .htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} mydomain1\.com$ [NC]
RewriteRule ^(.*)$ /home/myaccount/dom1/$1 [QSA,NE,L]

This gives me an error 500.

I hope you can point me in the right direction…

Thanks - Rowan

Rowan,

It’s not SitePoint’s protocol … it’s mine! There are many members (and staff) who share code. As a former instructor pilot, I’d rather see someone learn as it’s similar to the old “give a man a fish and you feed him for one day; teach him to fish and you feed him for a lifetime” so I rarely provide code without seeing at least an attempt to learn and code.

What’s your question about the RewriteRule? Simply put, it’s

RewriteRule {regex} {redirection} [{flags}]

regex: It’s PERL regex which is greatly simplified because the variables are limited to single lines of text. Moreover, each variable is strictly structured (as text or URI or …).

redirection: either an internal redirection (IN THE WEBSPACE - I think your /home/ is NOT in the webspace so that’s likely the cause of your problem) or external redirection (http://www.yadda.yadda/path/file).

flags: the modifiers dependent upon the directive.

Answers:

  • Yes, but it must be relative to the DocumentRoot.
  • Relative to the directory in which the .htaccess (mod_rewrite directive) resides
  • You’ve got the idea: It’s relative to the CURRENT LOCATION which, for simplicity, should be your DocumentRoot. If the redirection is index.php and mod_rewrite is in your DocumentRoot, it’ll serve the /home/myaccount/public_html/index.php file. The point of not using /index.php is that the server is supposed to look at your root directory FIRST so, if there is an index.php there, that is the one that will be selected, not %{DOCUMENT_ROOT}/index.php! IMHO, that should NOT be permitted so mod_rewrite should throw an error - but I’ve got to check on that!
  • I got ahead of myself with that explanation

RewriteBase was designed to UNDO a mod_alias Redirect. In other words, if you don’t have a Redirect going on, DON’T use RewriteBase! Besides, I see RewriteBase / all the time (in the DocumentRoot)! It does precisely NOTHING (but waste server cycles).

Direct outside the webspace? That’s a breach of security! So, that answer is an obvious NO! You can, however, include(); PHP scripts which are outside your webspace but you cannot link to them as they are NOT accessible via http.

Please use [ code ] … [ /code ] for your code:


RewriteEngine [COLOR="Blue"]o[/COLOR]n
[COLOR="Red"]RewriteBase /[/COLOR]
RewriteCond %{HTTP_HOST} mydomain1\\.com$ [NC]
# GOOD!  This is far better than ^(www\\.)?mydomain1\\.com$
RewriteRule ^[COLOR="Red"](.*)[/COLOR]$ [COLOR="Red"]/home/myaccount/[/COLOR]dom1/$1 [QSA,NE,L]

Because you’re using “Lazy Regex,” this will LOOP adding new dom1 directories until Apache discovers that you’ve caused it to loop. Be glad that you’re not using Apache 1.x 'cause you’d have to restart to break the loop! Add another exclusion (via RewriteCond) to prevent a redirection if dom1 subdirectory is present in the {REQUEST_URI} - then LEARN regex so you’ll know why you won’t just indiscriminately use (.*) when you want to match something.

Okay, enough bashing you as you ARE trying. Unfortunately, you did manage to generate many of the errors most other ‘noobies’ manage to generate, too. My “technique” (such as it is) is to make you THINK about the code so you’ll understand what the code is doing for you and why (IMHO, (.*) is NOT a sign of a thinker UNLESS it comes with enough “exclusions” to absolutely prevent looping) it works (or doesn’t work).

PLEASE ask questions on anything you don’t understand! mod_rewrite is a VERY powerful tool and, once the few basics are learned, you’ll be teaching others to “share the wealth” that your understanding will bring to your websites.

Regards,

DK