I have a problem with create pernament (301) redirect in apache:
I have 2 domains:
olddomain.com
with many subdomains
newdomain.com
and I want to do redirect like:
if user enter on http://anysubdomain.olddomain.com
should be redirected to http://anysubdomain.newdomain.com
if user enter on http://olddomain.com/something
should be redirected to http://newdomain.com/something
if user enter on http://olddomain.com/different/index.html
should be redirected to http://newdomain.com/different/index.html
if user enter on http://example.olddomain.com/ex/index.html
should be redirected to http://example.newdomain.com/ex/index.html
I don't know how I should set this:
<VirtualHost *:80>
ServerName olddomain.com
</VirtualHost>
Regards
To redirect all of your requests from the old domain to the new one, I would use the following.
RewriteCond %{HTTP_HOST} ^(.*\.)?olddomain\.com [NC]
RewriteRule ^(.*)$ http://%1newdomain.com/$1 [R=301,L]
It matches both the subdomain and request URI and merges them with the new domain. If there is no subdomain, it proceeds to match the request and will redirect without a subdomain.