apache - redirect subdomain to another domain - apache

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!

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.

Related

Apache2 - Using a lowercase subdomain to serve an uppercase folder

Currently the website being hosted uses the following structure of test.mysite.com/{site} which then serves /var/www/test/{site}
I've been trying to create a virtualhost that can match several subdomains and serve the folders under /var/www/test/. The urls would look like {site}.test.mysite.com
I've tried several approaches, one using virtual document root:
<VirtualHost *:80>
ServerName test.mysite.com
ServerAlias *.test.mysite.com
VirtualDocumentRoot /var/www/test/%1/
</VirtualHost>
This works well except that the folders under /var/www/test/ are in uppercase and I cant seem to get the %1 to uppercase.
A different approach was using mod rewrite as specified on the apache site
<VirtualHost *:80>
ServerName test.mysite.com
ServerAlias *.test.mysite.com
RewriteEngine on
RewriteMap upper int:toupper
RewriteMap lowercase int:tolower
RewriteCond ${lowercase:%{SERVER_NAME}} ^[a-z0-9-]+\.test\.mysite\.com$
RewriteRule ^([a-z0-9]+)\.test\.mysite\.com/$ /var/www/test/${upper:$1}/
</VirtualHost>
However it never seems to actually rewrite and just keeps serving the default apache document root.
I'd prefer not to have to change the name of all the folders to lowercase due to other dependencies. Are there any other options for doing this?
Your rule is wrong: RewriteRule, here, only apply to the full path of the URL which does not include the host. Try:
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+)\.test\.mysite\.com$
RewriteRule .* /var/www/test/${upper:%1}$0 [L]

Apache Configuration: redirecting to www in .htaccess

I have read a number of formulas for redirecting example.com to www.example.com and this appears to apply to .htaccess. However, I am confused about how this might work.
I want to assume that I have no access to the Apache vhosts configuration, and that I need to do it with .htaccess.
Suppose the configuration contains something like this:
<VirtualHost *:80>
ServerName www.example.com:80
ServerAlias www.example.com
VirtualDocumentRoot /whatever/example.com/www
</VirtualHost>
One such formula is something like this"
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ www\.%{HTTP_HOST}/$1 [R=301,L]
However, I don’t see how one root directory can respond to both requests.
The question is: Is it even possible to redirect example.com to www.example.com using .htaccess only, without tweaking the Virtual Host file?
The way I see it, the original request
I run my own server, and I can do anything I like. However, I am asking this because many of my clients and students have no access to tehe configuration, and can only fiddle with .htaccess.

Apache VirtualHosts

If apache was presented with the domain name origin.datingasia.co would it match both below VirtualHost entries?
<VirtualHost *:80>
ServerName datingasia.co
ServerAlias www.datingasia.co
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) http://www.%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/datingasia.co
ServerName origin.datingasia.co
ServerAlias origin.datingasia.co
</VirtualHost>
Also once a domain has the rewrite rule applied - eg: has 'http://www.etc.etct' added to it does it redirect automatically? would this occur before a DocumentRoot path would be used?
Only the second entry would be triggered for the domain "origin.datingasia.co". If you wanted the first one to trigger it as well, you would need to add a second ServerAlias parameter.
ServerAlias origin.datingasia.co
This would cause a problem though since your second entry contains that same ServerAlias. All ServerName/ServerAlias must be unique, otherwise Apache will not know which block to use for the request.
The first entry will only catch requests for "datingasia.co" and "www.datingasia.co". You're missing the DocuemntRoot though, in the event that the rewrite rule condition is not met (IE: www.datingasia.co). This will cause your requests to fail once they get to the www.datingasia.co page because Apache will not know what root to serve the requests from. The browser will automatically be redirected to "www.datingasia.co" when they visit "datingasia.co".
For the second entry, you do not need "ServerAlias origin.datingasia.co" since you already have that domain defined in the ServerName. You would only need a ServerAlias line if you wanted an additional unique domain to point to this host (IE: ServerAlias www.origin.datingasia.co).
Since your rewrite rules are in the host, if the condition is met Apache will not need the DocumentRoot. But if the condition is not met, Apache will attempt to serve the request and as a result will require the DocumentRoot. Your rule will forward "datingasia.co" to "www.datingasia.co" but will fail at "www.datingasia.co" because it is missing the DocumentRoot and does not meet the RewriteCond.

redirecting www.subdomain.example.com to subdomain.example.com

I've had some users trying to access a site that is registered as subdomain.example.com with www.subdomain.example.com.
is there some sort of .htaccess rule I can add to redirect people that arrive using www.subdomain.example.com to subdomain.example.com?
Also, do I have to change DNS stuff?
Sure, use a directive like:
<VirtualHost *:80>
ServerName www.subdomain.example.com
Redirect permanent / http://subdomain.example.com/
</VirtualHost>
Apache automatically preserves anything after the / when using the Redirect directive, which is a common misconception about why this method won't work (when in fact it does).
Also, yes you will need to change DNS records, because www.subdomain.example.com is a distinct hostname that needs its own A (or CNAME) record to point the browser to an appropriate server in the first place.
RewriteCond %{HTTP_HOST} ^www.subdomain.domain.com
RewriteRule (.*) http://subdomain.domain.com/$1 [R=301,L]
You need to add a virtual host directive in httpd.conf and Redirect Permament to the correct subdomain and add the additional DNS entry (CNAME is fine)

Redirect/rewrite alternative domains to root domain with apache2/htaccess

I 2 domains with and without the www prefix. When a user visits any of these domains, I want it to automatically reroute to a chosen 1 of them.
For example:
domain.com
www.domain.com
domain.co.uk
www.domain.co.uk
When a user visits www.domain.com, domain.co.uk or www.domain.co.uk, it will rewrite to domain.com
So far, I have my apache2 virtual host block setup like this:
<VirtualHost *:80>
ProxyPass / http://localhost:3060/
ProxyPassReverse / http://localhost:3060/
ServerName domain.com
ServerAlias www.domain.com
ServerAlias domain.co.uk
ServerAlias www.domain.co.uk
</VirtualHost>
But this doesn't do the rewriting/rerouting. I need to also make sure that it takes into account any paths. For example, www.domain.co.uk/test would change to domain.com/test
Any ideas how I can do this in the virtual host block? I'm assuming I would split the 3 domains to be rewritten into a separate block and treat them there, but really not sure how to accomplish all the rules I need.
Per your comment, you want to redirect the three aliases to the main domain, and you've stated that you would like to do this within the virtual host configuration.
<VirtualHost *:80>
...
RewriteEngine on
# If using one of the aliases ...
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk
# ... redirect to the main domain
RewriteRule ^(.*)$ http://domain.com/$1 [R=302,L]
</VirtualHost>
You can also add the Rewrite* directives in your domain's .htaccess file.
To make the redirect permanent, change 302 to 301 - this basically instructs browsers and search engines to cache the redirect.