Redirect Web Traffic to Another Sub-domain (e.g. www) or HTTP to HTTPS
Using the mod_rewrite
module you can configure Apache to redirect web traffic from one sub-domain to another. Usually used to redirect a bare domain (http://xensoft.com) to the www
sub-domain (http://www.xensoft.com).
First make sure you're loading the mod_rewrite module in your httpd.conf
or a conf.d
file:
LoadModule rewrite_module modules/mod_rewrite.so
Then in the directory configuration for your site add the following:
<Directory /dir/location/XenSoft.com>
...
# Turn rewrite engine on
RewriteEngine on
# Set regex we'll match
RewriteCond %{HTTP_HOST} ^xensoft.com
# Set where we're redirect to (301 response)
RewriteRule (.*) http://www.xensoft.com/$1 [R=301,L]
...
</Directory>
You can also add these rules to .htaccess
files if you make use of them, or your site's <VirtualHost>
configuration.
You can check that you're getting the redirect command in the HTTP
header, give it a try on http://xensoft.com.
You can do the same thing to redirect to HTTPS
to make sure your site it accessed using SSL
.
<Directory /dir/location/XenSoft.com>
...
# if https isn't being used
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
...
</Directory>
Apache will process all rules that match their conditions, so you can write rules to fix individual problems and have them chains to resolve multiple issues. For example, using the above rules you can have the sub-domain fixed then redirect to HTTPS
.