views:

34

answers:

1

Hi,

I need to redirect every subdomain into the domain it belongs changing the subdomain name into a parameter using mod_rewrite, and I'm not sure how. Also, I need to "reindex" the parameters so that the subdomain name becames the first parameter of the uri and the other parameters of the uri follow it by their own order. Something like this

category.domain.com/search/flowers

to

domain.com/category/search/flowers

Any thoughts on how to achieve this using mod_rewrite?

Cheers!

+1  A: 

You can do this with one VirtualHost for all of the subdomains:

<VirtualHost *:80>
    ServerName category.domain.com
    ServerAlias foo.domain.com bar.domain.com

    RewriteEngine On
    RewriteCond %{HTTP_HOST} (.*).domain.com
    RewriteRule (.*) http://domain.com/%1$1 [R=301,QSA,L]
</VirtualHost>

For it to work properly, you must have something set as the ServerName, so just choose one and list the rest of your subdomains on the ServerAlias line.

You can have multiple ServerAlias lines, so you could break them into multiple lines for readability if you have a large number of subdomains.

In the RewriteRule, the %1 matches the first matched pattern in preceding RewriteCond lines.

bradym
I don't have access to that, just to an ordinary .htaccess
yoda
In that case, just put the Rewrite stuff in the .htaccess for each subdomain. Assuming you don't have anything else in the .htaccess you need to keep for individual subdomains and you can create symlinks, you could put that stuff in the .htaccess for one subdomain, and create symlinks from the other subdomains. That way you don't have to repeat it in multiple places.
bradym