Reputation: 1
Hi I need to redirect all subdomains in a domain to the same subdomain but at a different domain. The best way im guessing is through a htaccess file but im not sure how the file would be.
Example:
sd1.example.net ---> sd1.example.com
sd2.example.net ---> sd2.example.com
sd3.example.net ---> sd3.example.com
But I need this to be done for all of the subdomains in example.net. Thanks.
Upvotes: 0
Views: 2003
Reputation: 4336
If you have an Apache server running on example.net
and the requests for all the subdomains look in the same parent directory you can do something like the following:
RewriteEngine On
### Find the subdomain part (it will be available in %1)
### Use one of the RewriteCond-s and delete the other one
# Only redirect subdomains, not plain example.net
RewriteCond %{HTTP_HOST} ^(.*)\.example\.net$
## Redirect both subdomains and plain example.net (uncomment to enable)
#RewriteCond %{HTTP_HOST} ^(.*\.)?example\.net$
# Find the path requested (it will be available in $0)
# This rule does not attempt to match the domain, only the path
# Redirect subdomain and path to example.com
RewriteRule ^.*$ http://%1.example.com/$0 [L]
I haven't tested this so it might be missing query strings, etc. It will also undesirably redirect https://
to http://
. As long as you have a single .htaccess file that can affect all your subdomains this should work, or at least be a very good starting point. Check out Apache's mod_rewrite documentation for more information about how this works.
Having recently wanted to do exactly this myself recently, I have worked out a short .htaccess file that does the trick:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*\.)?olddomain\.com$
RewriteRule ^(.*?/)?public_html/(.*)?$ "http\:\/\/%1newdomain\.org\/$2" [R=301,NE,L]
It assumes the following file structure:
.htaccess
public_html/
+-content
lots/
+-public_html/
| +-content
of/
+-public_html/
| +-content
subdomains/
+-public_html/
+-content
My main site (newdomain.org
) is in /public_html/
. I have a number of subdomains, e.g. subdomains.newdomain.org
which is in /subdomains/public_html/
. This keeps all the files of each my subdomains completely separate from each other and my main site. (My hosting service recommends /public_html/
, /public_html/subdomains/
but that means each subdomain is also accessible at newdomain.org/subdomains/
which is not what I want). The only restriction this gives me is that I can never have a subdomain called public_html
, which I think you'll agree is perfectly acceptable.
The flags on the rule are as follows:
R=301
- Redirect with a 301 Moved Permanently
code. You can change the code if you don't need a permanent redirect, e.g. 302
.NE
- No Encoding - Don't URI encode the new address, i.e. keep %
as %
, not %25
L
- Last - Stop processing rulesNote that the .htaccess
file must be in the root directory of your web server, not in the directories with your content files. This is because the rewrite rule works at the file system level, not the URL address level.
An address:
is changed to:
Upvotes: 1