Redirecting subdomain to https
June 11, 2008 2:44 PM   Subscribe

I need to redirect all traffic on a subdomain to https instead of http. mod_rewrite is installed, but I don't know how to use it.

So for a couple different subdomains I need to force https in other words I want any requests to:

http://sub1.example.com

to go to:
https://sub1.example.com

I found a couple examples online, but I'm not familiar enough with mod_rewrite and .htaccess files enough to make it work. Help, I'm clueless!
posted by The Radish to Computers & Internet (7 answers total) 4 users marked this as a favorite
 
You don't really need mod_rewrite to do this. You can just use redirect. I have:

Redirect /webmail https://www.foo.org/webmail

But you could just do:

Redirect / https://www.mydomain.com
posted by autojack at 2:49 PM on June 11, 2008


Incidentally, you can just put that line in a file called .htaccess in the web root of your site, or you can put it in the httpd.conf file.
posted by autojack at 2:50 PM on June 11, 2008


Using mod_rewrite:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

Will redirect every link to an http:// address to the same url, but with https://
posted by meta_eli at 3:04 PM on June 11, 2008


Best answer: Ah, but it take it you want to do this for only certain subdomains?

Then try something like:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(sub1|sub2|sub3)\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

The first line turns on mod_rewrite (if it isn't already). The next line says that the next RewriteRule should only trigger if is not an HTTPS request. The line after that adds a condition that only requests to hostnames starting with sub1. or sub2. or sub3. will apply to the next RewriteRule the [NC] stands for No Case -- case insensitive. And the RewriteRule says to redirect any url (.*) to the same hostname and url but with https:// in front. (The [R] stands for Redirect, the [L] means "Last" -- don't bother looking for any more rules).

This can usually go in either .htaccess file or in the Apache config file.
posted by meta_eli at 3:09 PM on June 11, 2008


Response by poster: I tried putting your code in the .htaccess file in both the subdomain folder and the main documetn root but it didn't work. Any clue as to what I'm doing wrong?
posted by The Radish at 3:50 PM on June 11, 2008


Response by poster: Wait, there must have been an incorrect line break or something in the file. I used vi to edit the file and it seems to be working now. Thanks!
posted by The Radish at 3:54 PM on June 11, 2008


typically .htaccess files are disallowed by default

you could always add a config file to

/etc/httpd/conf.d/

e.g. mysite.conf
posted by idb at 3:58 PM on June 11, 2008


« Older Easy Linux versions   |   Southampton - fun and cheap? Newer »
This thread is closed to new comments.