Forum Moderators: phranque
The mod rewrite should do the work:
www.example.com/xyz -> xyz.example.com
I don't want to send a redirect to the browser(so it doesn't know the page is different).
Another example, just to clarify.
If http://www.example.com/xyz/urlpath is entered into the browser, Apache returns the contents of http://xyz.example.com/urlpath
I'm not familiar with mod rewrite.
Hope someone can help.
[edited by: Robert_Charlton at 4:03 am (utc) on Feb. 28, 2009]
[edit reason] changed to example.com - it can never be owned [/edit]
*** I don't want to send a redirect to the browser. ***
You'll need a
rewrite using a RewriteRule then. It uses very similar syntax to a redirect; but for a rewrite the target should not contain a domain name. Use [L] at the end of the rule. Post your best effort code here with details of what problems you are having.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^bookings/(.*) http://bookings.example.com/$1
The problem is that after entering in the address bar:
http://www.example.com/bookings/phpquery
I get in the browser the content of:
http://booking.example.com/phpquery, so it's ok,
BUT (!)
the address bar changed at the same moment and is also displaying: http://booking.example.com/phpquery
should I use [P] flag?
I should have mentioned that bookings is a CNAME which points to another domain which i'm affiliated with. So i'm pulling the content from provider website, which is prepared for me with my logo etc.
I could stay with the solution bookings.example.com, but I will have also other CNAMEs, so solution www.example.com/<various CNAMEs>/ is better for SEO and customers
[edited by: jdMorgan at 4:22 pm (utc) on Mar. 3, 2009]
[edit reason] de-linked [/edit]
The first rule externally redirects to canonicalize all hostnames to either www.example.com, or to xyz.example.com, no matter what variations of the hostname are requested. This simplifies the following rules, as well as others which you may wish to add, and prevents duplicate-content problems.
Examples of non-canonical hostnames redirected by this rule are
example.com --301--> www.example.com
www.example.com. --301--> www.example.com
www.example.com:80 --301--> www.example.com
www.example.com.:80 --301--> www.example.com
www.xyz.example.com --301--> xyz.example.com
xyz.www.example.com --301--> xyz.example.com
www.xyz.www.example.com --301--> xyz.example.com
etc.
The second rule externally redirects all direct client requests for the subdomain-subdirectories back to the subdomain, so that clients requesting www.example.com/xyz/<something> will be redirected to xyz.example.com/<something>. Again, this is to avoid duplicate content, and also to prevent "fishing" in your directory structure by possibly-unfriendly prying eyes. This redirect can only be done based on direct client requests, otherwise, it would interact with the third rule creating an 'infinite' redirect/rewrite loop.
The third rule internally rewrites all subdomain requests except for "www" to a subdirectory whose name is "sd_" plus the subdomain name. The "sd_" is an arbitrary "tag" needed to identify subdomain subdirectories to prevent recursion and also so that you can still have "real" subdirectories not associated with a subdomain. So you can change "sd_" to any unique string you like, as long as it does not match *any* other subdirectory name.
You will need to rename all of your subdomain-subdirectories so that they start with "sd_" in order for any of this to work. There are other ways to do it, but all are far more painful. I suggest that you test and get this working as-is, then make changes if you feel you need to.
# Canonicalize all requested hostnames
RewriteCond www>%{HTTP_HOST} ^(www)>example\.com [OR]
RewriteCond %{HTTP_HOST} ^(www)\.example\.com\. [OR]
RewriteCond %{HTTP_HOST} ^(www)\.example\.com:[0-9] [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.(www\.)?example\.com [OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.example\.com
RewriteRule (.*) http://%1.example.com/$1 [R=301,L]
#
# Externally redirect direct client requests for www.example.com/sd_xyz to xyz.example.com
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sd_[^/]+/
RewriteRule ^sd_([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
#
# Internally rewrite xyz.example.com to filepath example.com/sd_xyz, except for "www.example.com"
RewriteCond $1 !^sd_
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /sd_%1/$1 [L]
Jim
[edit] Correction to last RewriteCond in first rule as noted below. [/edit]
[edited by: jdMorgan at 4:33 pm (utc) on Mar. 3, 2009]
Thanks a lot for reply and the effort you put into it.
I tried it and unfortunately it returns error 404.
I tried also only the third part and the effect is the same.
You wrote in a comment to 3rd rule that
# Internally rewrite xyz.example.com to filepath example.com/sd_xyz, except for "www.example.com"
I'm not sure if this is right.
I want the opposite.
When customer browser requests for:
www.example.com/bookings/phpquery
(or www.example.com/sd_bookings/phpquery)
the apache should give him the content of:
bookings.example.com/phpquery
In fact my folder bookings doesn't really exist, because it's just for search engines robots, to avoid multiple subdomains.
Best Regards,
Nick
ServerAlias www.example.co.uk *.example.co.uk
DocumentRoot /var/www/example.co.uk/public
DirectoryIndex index.html index.php
<Directory /var/www/example.co.uk/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
And this is the script I was trying to use:
# Canonicalize all requested hostnames
RewriteCond www>%{HTTP_HOST} ^(www)>example\.co\.uk [OR]
RewriteCond %{HTTP_HOST} ^(www)\.example\.co\.uk\. [OR]
RewriteCond %{HTTP_HOST} ^(www)\.example\.co\.uk:[0-9] [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.(www\.)?example\.co\.uk [OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.(www\.)?example\.co\.uk
RewriteRule (.*) [%1.example.co.uk...] [R=301,L]
#
# Externally redirect direct client requests for www.example.com/sd_xyz to xyz.example.com
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sd_[^/]+/
RewriteRule ^sd_([^/]+)/(.*)$ [$1.example.co.uk...] [R=301,L]
#
# Internally rewrite xyz.example.com to filepath example.com/sd_xyz, except for "www.example.com"
RewriteCond $1 !^sd_
RewriteCond %{HTTP_HOST} !^www\.example\.co\.uk
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.co\.uk
RewriteRule (.*) /sd_%1/$1 [L]
I'm pretty stumped here, any suggestions would be MORE than welcome. Thanks guys.
www.test.example.com goes to www.example.com
so there has to be something in the first rule that causing an infinite loop
i also found something interesting has anyone ever read about this before?
www.merhar.si/hosting/mapping-subdomains-to-subdirectories-with-apache-the-easy-way
[edited by: jdMorgan at 4:25 pm (utc) on Mar. 3, 2009]
[edit reason] De-linked. [/edit]
RewriteCond %{HTTP_HOST} ^([^.]+)\.(www\.)?example\.com RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.example\.com
Jim