Forum Moderators: phranque

Message Too Old, No Replies

URL rewriting for virtual host - without using httpd.conf

How is this correctly done?

         

Trav

5:31 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



Hello,

I have spent about 2 days searching this topic, and have not found an answer that speaks to my exact need.

Here's the situation: I have a primary domain, www.example.com, that resolves to my server's root (/). I also have a number of other domains (www.2nd-example.com, www.3rd-example.com, etc.), all of which must resolve to the same subdirectory within the root (/others/).

I have a rewrite in place which accomplishes most of this, but the browser's address bar shows the actual server path, i.e. www.example.com/others/

I want the address bar to show the requested domain without the directory instead, and not the primary domain/directory. Essentially simulating the 'Virtual Hosts' feature of httpd.conf. (we don't have access to that file on our hosting, or else I'd do it there.)

How is this done? many thanks in advance!

jdMorgan

6:05 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How, specifically, are the 'other' domains 'mapped' to the /others/ subdirectory?

It sounds like the code (or other mechanism) used to do this is faulty, as it should internally rewrite requests for those domains to the subdirectory, rather than externally redirecting them.

An external redirect involves sending an HTTP response to the client (browser or robot) asking it to start a new HTTP transaction and ask again for the previously-requested resource using a new URL, and so changes the browser's address bar; An internal rewrite operates entirely within the context of the current HTTP transaction, and does not inform the client that the content is being served from a 'non-default' location in the server's filesystem.

An external redirect is a URL-to-URL translation, while an internal rewrite is a URL-to-filepath translation.

Jim

Trav

6:12 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



Thanks for your response jd. Here's a snippet from the htaccess file.

RewriteCond %{HTTP_HOST} ^www.example2.com
RewriteRule ^(.*)$ http://www.example.com/others/ [R=permanent,L]

I totally get what you're saying, I just haven't found an example of how to to this redirect and rewrite correctly. Essentially the code above works, but it leaves the original domain in the browser. I'd like it to display the content in the /others/ directory, while showing only www.example2.com/whatever-file.htm in the address bar.

Many thanks!

jdMorgan

6:35 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Simply change the syntax from a redirect to a rewrite, and add a back-reference (which should have been in the original code as well) so that more than just the 'home page' at "/" in the alternate domains is accessible:

RewriteCond $1 !^others/
RewriteCond %{HTTP_HOST} ^www\.example2\.com
RewriteRule (.*) /others/$1 [L]

(Other changes for efficiency/robustness)

If you want the add-on domains to have their own subdirectories, you could use either:


RewriteCond $1 !^others/
RewriteCond %{HTTP_HOST} ^www\.(example2\.com)
RewriteRule (.*) /others/%1/$1 [L]

or

RewriteCond $1 !^addon_
RewriteCond %{HTTP_HOST} ^www\.(example2\.com) [OR]
RewriteCond %{HTTP_HOST} ^www\.(example3\.com) [OR]
RewriteCond %{HTTP_HOST} ^www\.(example4\.com)
RewriteRule (.*) /addon_%1/$1 [L]

In all three cases, the added initial RewriteCond stops a potential 'infinite' rewrite loop.

I structured the code assuming that you will add additional [OR]ed RewriteConds to each rule to test HTTP_HOST for the other domains, as in the final example. Therefore, the parenthesized domain name is back-referenced in the RewriteRule using the %1 variable.

Be sure to completely flush your browser cache before testing.

Jim

Trav

6:46 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



Hi Jim,

Thanks again. For some reason that directive ends up causing a 500 error, and breaking the primary domain. Any thoughts?

Trav

jdMorgan

7:20 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What specifically causes the problem? I don't understand what you mean by "directive" as there are ten of them in the code above; What did you put on your server?

Jim

Trav

7:29 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



Sorry for that- here's what I have going, which causes the primary domain to be unhappy. Note that the primary domain in this case is www.example.com, and should resolve to server root (/); the additional domain is www.example1.com, and should resolve to www.example.com/others/, while displaying www.example1.com in the address bar:

# define an error page
ErrorDocument 404 /404.htm

# enable Symbolic links
# Options +FollowSymlinks

# turning rewrite engine on
RewriteEngine on

RewriteCond $1 !^others
RewriteCond %{HTTP_HOST} ^(example1\.net) [OR]
RewriteCond %{HTTP_HOST} ^www\.(example1\.net) [OR]
RewriteRule (.*) /others%1/$1 [L]

Thanks so much for your patient and considerate responses!

Trav

Trav

7:31 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



edit- the second domain above should be 'www.example1.net' - I'll be including some .net's and .com's, and got my example screwed up : )

jdMorgan

8:18 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You put an extra [OR] in that code -- You cannot [OR] a RewriteCond with a RewriteRule, so make sure the final RewriteCond does not have an [OR] on it -- See my example above. Then try testing again -- after flushing your browser cache, of course.

Jim

Trav

8:45 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



Jim you are the best.

In follow up, I wanted to see if I'm understanding this; it seems like what I was missing before were the parentheses surrounding the domains I wanted to redirect.. is this because I am essentially defining them as variables?

Thanks so much for your help!

trav

jdMorgan

9:08 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Parentheses in RewriteConds and RewriteRules are used to apply quantifiers to groups of characters or to sub-patterns, and --more importantly here-- to create back-references. The parameters in the request matching the parenthesized subpatterns then become available for use (the can then be "back-referenced") in the substitution URL, and in other RewriteConds following the one where they are defined.

Jim

Trav

9:37 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



I think I see the distinction - I'm clearly going to need to do some serious reading on this! Really, thanks big-time Jim, and hope I can return the favor sometime.

Trav