Multiple domain names mapping to different base paths

Bit of a head banger this one (for me at least), but from what I read I believe it’s possible with modrewrite. I’d be really grateful for some help as it’s a real blocker for me - I have had a tip off that I might need.

First what I’m trying to achieve, I hope this is clear…

should remain in the browser address bar as it is but serve the resource here…
base_url/properties/1

should remain in the browser address bar as it is but serve the resource here…
base_url/properties/2

and a catch all so that…

http://www.bar.co.uk/full_details
should remain in the browser address bar as it is but serve the resource here…
base_url/properties/2/full_details (or whatever’s put after the /2/)

The following code is working to a point but the URLs in the browser address bar are not being rewritten, i.e. I see http://www.bar.co.uk/properties/2/full_details rather than http://www.bar.co.uk/full_details



<VirtualHost *:80>

  ServerName  foo.co.uk
  ServerAlias www.foo.co.uk

  DocumentRoot /home/xxxx/public_html/bookings/public

  Options +FollowSymLinks
  RewriteEngine on
  RewriteRule ^/$ /properties/1/$1 [R]

</VirtualHost>

<VirtualHost *:80>

  ServerName  bar.co.uk
  ServerAlias www.bar.co.uk

  DocumentRoot /home/xxxx/public_html/bookings/public

  Options +FollowSymLinks
  RewriteEngine on
  RewriteRule ^/$ /properties/2/$1 [R]

</VirtualHost>


As I say, grateful for insight - would love to solve this one.

  1. ok tried that. It worked, got the right page displayed but still the path remains on the end /properties/1. I’m trying to have that removed.

  2. ok.

bb,

  1. Your first eight rows of low show that the redirection to properties/1/ was made. What’s the problem? Use an R=301,L to have that displayed to you (you can remove the R=301 when you go live with it).

  2. That’s what I’ve called the “Missing Support Files” problem - see the tutorial in my signature for the two possible solutions.

Regards,

DK

  1. Remove the R=301 … now that you’ve seen that it works.

  2. :smiley:

Regards,

DK

bb,

Yes, but it looks like I’m conflicted over whether or not to include the leading / in the redirection. They should both be the same to please add (then test again) a / in front of properties in the FIRST RewriteRule.

Regards,

DK

David,

Thanks very much for this. It certainly looks like you’ve got a handle on it - I’m trying it later today. Your initial understanding of my objective is correct, to reiterate…

foo.co.uk/ => properties/1
bar.co.uk/ => properties/2
and
bar.co.uk/full_details => properties/2/full_details
(or whatever’s put after the /2/)

The only thing I was surprised by in your example (code extract below) was the specific reference to “full_details” in the code. After the “/1/” or “/2/” I have many menu options including “photos”, “contact_us” etc, the usual suspects - thought taht it might be possible to trap the bit after the “/1/” and use it…If it’s necessary for me to verbosely and explicitly definine each “menu option” that’s ok but not as good I guess. Phew, it’s tricky to explain this stuff, hoping it’s clear.


RewriteCond %{HTTP_HOST} foo\\.co\\.uk [NC]
RewriteCond %{REQUEST_URI} !full_details
RewriteRule full_details/ properties/2/full_details [L]
# Note, there is NOTHING after full_details to to redirect to your "whatever"

Later today though, I’ll try your code and see where I get it. thanks.

Well that works to a point (it renders something)… two main problems

  1. www.chaletcordee.co.uk ends up at the site root not at the specific property page…http://www.chaletcordee.co.uk/properties/1

  2. all the stylesheets are busted, log shows it’s not looking in the right place.

log output




87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (2) init rewrite engine with requested uri /
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (3) applying pattern '^/(.*)$' to uri '/'
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (4) RewriteCond: input='www.chaletcordee.co.uk' pattern='chaletcordee\\.co\\.uk' [NC] => matched
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (4) RewriteCond: input='/' pattern='!properties/1' => matched
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (2) rewrite '/' -> '/properties/1/'
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (2) local path result: /properties/1/
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (2) prefixed with document_root to /home/rupert/public_html/bookings/public/properties/1/
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (1) go-ahead with /home/rupert/public_html/bookings/public/properties/1/ [OK]
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#2632e98/initial] (2) init rewrite engine with requested uri /stylesheets/formtastic.css
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#2632e98/initial] (3) applying pattern '^/(.*)$' to uri '/stylesheets/formtastic.css'
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#2632e98/initial] (4) RewriteCond: input='www.chaletcordee.co.uk' pattern='chaletcordee\\.co\\.uk' [NC] => matched
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#2632e98/initial] (4) RewriteCond: input='/stylesheets/formtastic.css' pattern='!properties/1' => matched
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#2632e98/initial] (2) rewrite '/stylesheets/formtastic.css' -> '/properties/1/stylesheets/formtastic.css'
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#2632e98/initial] (2) local path result: /properties/1/stylesheets/formtastic.css
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#2632e98/initial] (2) prefixed with document_root to /home/rupert/public_html/bookings/public/properties/1/stylesheets/formtastic.css
87.82.96.70 - - [23/Oct/2010:07:49:07 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#2632e98/initial] (1) go-ahead with /home/rupert/public_html/bookings/public/properties/1/stylesheets/formtastic.css [OK]
87.82.96.70 - - [23/Oct/2010:07:49:08 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (2) init rewrite engine with requested uri /stylesheets/formtastic_changes.css
87.82.96.70 - - [23/Oct/2010:07:49:08 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (3) applying pattern '^/(.*)$' to uri '/stylesheets/formtastic_changes.css'
87.82.96.70 - - [23/Oct/2010:07:49:08 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (4) RewriteCond: input='www.chaletcordee.co.uk' pattern='chaletcordee\\.co\\.uk' [NC] => matched
87.82.96.70 - - [23/Oct/2010:07:49:08 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (4) RewriteCond: input='/stylesheets/formtastic_changes.css' pattern='!properties/1' => matched
87.82.96.70 - - [23/Oct/2010:07:49:08 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (2) rewrite '/stylesheets/formtastic_changes.css' -> '/properties/1/stylesheets/formtastic_changes.css'
87.82.96.70 - - [23/Oct/2010:07:49:08 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (2) local path result: /properties/1/stylesheets/formtastic_changes.css
87.82.96.70 - - [23/Oct/2010:07:49:08 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (2) prefixed with document_root to /home/rupert/public_html/bookings/public/properties/1/stylesheets/formtastic_changes.css
87.82.96.70 - - [23/Oct/2010:07:49:08 +0100] [www.chaletcordee.co.uk/sid#25ccee0][rid#262ee78/initial] (1) go-ahead with /home/rupert/public_html/bookings/public/properties/1/stylesheets/formtastic_changes.css [OK]




bb,

Without the R=301 (or an external absolute redirection, i.e., http://yadda-yadda), the redirection should NOT be visible. Period.

Problem 1: Where did foo and bar go? It now looks like you’re dealing only with a single domain rather than co-located domains.

Problem 2: Where did properties/1 come from? I thought you were redirecting from domain/ to properties/1/ and that will NOT display properties/1 UNLESS you use an external redirection or 301.

Remember, I had you insert the 301 ONLY to verify that the redirection that you thought wasn’t working indeed was! Once confirmed, remove it and work on the missing support files, i.e., adding the <base> tag OR using absolute links.

Regards,

DK

removed the R=301 stuff but it still won’t strip the /properties/1 from the end of the URL in the browser.

Total dead end.

I missed your major point here also…apologies…

Now, after all that, I have to apologize for not saying what had been on my mind in the first place: It’s MUCH easier to POINT a domain at the correct physical directory than to redirect (and retain the relative links, too). That goes back to your VirtualHost definitions which is where I recommend that you start (if at all possible).

Now that does make sense - I can see how that would be a more straightforward and maintainable solution. In this case though it wouldn’t work, this is a single Ruby on Rails application generating the specific “properties/1/” “properties/2/” links, as such there is only one physical directory. If I’ve undertood it correctly, I couldn’t do this and I need to rewrite. Keep thinking this is very close!

bb,

Specificity! Okay, full-details is not a specific code you’re using but a set of lowercase letters and underscores to match. Therefore, replace full_details in the regex with ([a-z_]+) and $1 in the redirection and that should do the trick for you. I.e.,

RewriteEngine on

RewriteCond %{HTTP_HOST} foo\\.co\\.uk [NC]
RewriteCond %{REQUEST_URI} !properties/1
RewriteRule (.*) properties/1/$1 [L]

RewriteCond %{HTTP_HOST} foo\\.co\\.uk [NC]
RewriteCond %{REQUEST_URI} !$1
RewriteRule ^([a-z_]+)$ properties/2/$1 [L]
# $1 in the RewriteCond is a forward "backreference"
# to the atom created in the RewriteRule

RewriteCond %{HTTP_HOST} bar\\.co\\.uk [NC]
RewriteCond %{REQUEST_URI} !(properties/2|properties/2/full_details)
RewriteRule ^([a-z_]+)$ properties/2/$1 [L]

That was likely my confusion over the full_details as I’d added a / after that in the regex. Please provide a test URI so I can tell what you’re after.

Now, after all that, I have to apologize for not saying what had been on my mind in the first place: It’s MUCH easier to POINT a domain at the correct physical directory than to redirect (and retain the relative links, too). That goes back to your VirtualHost definitions which is where I recommend that you start (if at all possible).

Regards,

DK

thanks - the logs show me that the / thing you did does solve the extra / problem - but damn and blast the thing still doesn’t work, browser renders a 400 Bad Request error “Bad Request Your browser sent a request that this server could not understand”…

Losing will to live - thanks for bearing with me - lost and unsure if this is fixable this way.

rules in place (latest you posted)



  RewriteCond %{HTTP_HOST} chaletcordee\\.co\\.uk [NC]
  RewriteCond %{REQUEST_URI} !properties/1
  RewriteRule ^/(.*)$ properties/1/$1 [L]

  RewriteCond %{HTTP_HOST} chaletcordee\\.co\\.uk [NC]
  RewriteCond %{REQUEST_URI} !$1
  RewriteRule ^/([a-z_]+)$ /properties/1/$1 [L]


log output



83.244.227.78 - - [22/Oct/2010:12:32:55 +0100] [www.chaletcordee.co.uk/sid#1611ee0][rid#1673e78/initial] (2) init rewrite engine with requested uri /
83.244.227.78 - - [22/Oct/2010:12:32:55 +0100] [www.chaletcordee.co.uk/sid#1611ee0][rid#1673e78/initial] (3) applying pattern '^/(.*)$' to uri '/'
83.244.227.78 - - [22/Oct/2010:12:32:55 +0100] [www.chaletcordee.co.uk/sid#1611ee0][rid#1673e78/initial] (4) RewriteCond: input='www.chaletcordee.co.uk' pattern='chaletcordee\\.co\\.uk' [NC] => matched
83.244.227.78 - - [22/Oct/2010:12:32:55 +0100] [www.chaletcordee.co.uk/sid#1611ee0][rid#1673e78/initial] (4) RewriteCond: input='/' pattern='!properties/1' => matched
83.244.227.78 - - [22/Oct/2010:12:32:55 +0100] [www.chaletcordee.co.uk/sid#1611ee0][rid#1673e78/initial] (2) rewrite '/' -> 'properties/1/'
83.244.227.78 - - [22/Oct/2010:12:32:55 +0100] [www.chaletcordee.co.uk/sid#1611ee0][rid#1673e78/initial] (2) local path result: properties/1/


bb,

Problem #1 is that most people do not have access to the server/VirtualHost configuration files so my examples were for .htaccess (emphasis added to post #2). To change that code for use in the configuration files, you must add a / after the start anchor. Sorry for not stressing that earlier.

RewriteEngine on

RewriteCond %{HTTP_HOST} chaletcordee\\.co\\.uk [NC]
RewriteCond %{REQUEST_URI} !properties/1
RewriteRule ^/(.*)$ properties/1/$1 [L]

RewriteCond %{HTTP_HOST} chaletcordee\\.co\\.uk [NC]
RewriteCond %{REQUEST_URI} !$1
RewriteRule ^/([a-z_]+)$ /properties/1/$1 [L]

Rails sounds like a PITA CMS but, “if it ain’t broke, don’t fix it!”

Regards,

DK

Quick test…

here’s the real domain the rules and the error output.

getting bad request at the mo - www.chaletcordee.co.uk

Looks like an extra slash is getting in there or maybe another problem ?



error log

87.82.96.70 - - [22/Oct/2010:06:44:14 +0100] [www.chaletcordee.co.uk/sid#1822db0][rid#1886e78/initial] (2) init rewrite engine with requested uri /
87.82.96.70 - - [22/Oct/2010:06:44:14 +0100] [www.chaletcordee.co.uk/sid#1822db0][rid#1886e78/initial] (3) applying pattern '(.*)' to uri '/'
87.82.96.70 - - [22/Oct/2010:06:44:14 +0100] [www.chaletcordee.co.uk/sid#1822db0][rid#1886e78/initial] (4) RewriteCond: input='www.chaletcordee.co.uk' pattern='chaletcordee\\.co\\.uk' [NC] => matched
87.82.96.70 - - [22/Oct/2010:06:44:14 +0100] [www.chaletcordee.co.uk/sid#1822db0][rid#1886e78/initial] (4) RewriteCond: input='/' pattern='!properties/1' => matched
87.82.96.70 - - [22/Oct/2010:06:44:14 +0100] [www.chaletcordee.co.uk/sid#1822db0][rid#1886e78/initial] (2) rewrite '/' -> 'properties/1//'
87.82.96.70 - - [22/Oct/2010:06:44:14 +0100] [www.chaletcordee.co.uk/sid#1822db0][rid#1886e78/initial] (2) local path result: properties/1//



CURRENT RULES AM TRYING for one of the hosts - figured get one working.

<VirtualHost *:80>

  ServerName  chaletcordee.co.uk
  ServerAlias www.chaletcordee.co.uk

  #ServerName  apartmentnicole.co.uk
  #ServerAlias www.apartmentnicole.co.uk

  DocumentRoot /home/rupert/public_html/bookings/public

  AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/x-javascript


# old working stuff - but it's not "rewritten"
#  Options +FollowSymLinks
#  RewriteEngine on
#  RewriteRule ^/$ /properties/1/$1 [NS,QSA]
#  RewriteRule ^www.chaletcordee.co.uk$ www.chaletcordee.co.uk/properties/1 [NS,QSA]

#RewriteEngine on
Options +FollowSymLinks
RewriteEngine on

#
# ONLY FOR TESTING REWRITE RULES!!!!!
#
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 5

RewriteCond %{HTTP_HOST} chaletcordee\\.co\\.uk [NC]
RewriteCond %{REQUEST_URI} !properties/1
RewriteRule (.*) properties/1/$1 [L]

RewriteCond %{HTTP_HOST} chaletcordee\\.co\\.uk [NC]
RewriteCond %{REQUEST_URI} !$1
RewriteRule ^([a-z_]+)$ properties/1/$1 [L]
# $1 in the RewriteCond is a forward "backreference"
# to the atom created in the RewriteRule

</VirtualHost>





Not sure again, I tried the rules and it didn’t do what I thought it might.

I was hoping that after I put the rules in place going to

http://www.foo.com/properties/1

would show in the browser as…

http://www.foo.com/

But it didn’t, actually generated an error message…
hmmmm…from the logs I think I was getting a double / on the end of the URI. I eliminated that but still not right, although i’m away from my test rig so can’t give the specfic result.

bb,

First, WELCOME to SitePoint’s Apache forum!

Now, it appears that you’ve set both domains to the same DocumentRoot. Therefore, mod_rewrite is your best option to control what happens to a request which must be dependent upon the host. Therefore …

foo.co.uk/ => properties/1

bar.co.uk/ => properties/2

and

bar.co.uk/full_details => properties/2/full_details (or whatever’s put after the /2/)

.htaccess-wise (for testing):

RewriteEngine on

RewriteCond &#37;{HTTP_HOST} foo\\.co\\.uk [NC]
RewriteCond %{REQUEST_URI} !properties/1
RewriteRule (.*) properties/1/$1 [L]

RewriteCond %{HTTP_HOST} foo\\.co\\.uk [NC]
RewriteCond %{REQUEST_URI} !full_details
RewriteRule full_details/ properties/2/full_details [L]
# Note, there is NOTHING after full_details to to redirect to your "whatever"

RewriteCond %{HTTP_HOST} bar\\.co\\.uk [NC]
RewriteCond %{REQUEST_URI} !(properties/2|properties/2/full_details)
RewriteRule (.*) properties/2/$1 [L]

Not much for me to go on but that should give you a start.

Regards,

DK