3

I'm trying to redirect the following conditions:

http://mydomain.com
http://www.mydomain.com

to:

https://mydomain.com

But I don't want it to mess up anything with subdomains, so that someone who types:

http://m.mydomain.com

would not be redirected to https. My present htaccess file looks like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

I've tried using some of the examples I've found here on stack overflow, such as this one:

RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]

But although it appears to have worked for that person, it resulted in a "redirect loop" for me. As an aside, after I get this working I need to add some redirects to handle subdomains, to invisibly (without changing the URL) redirect http://m.mydomain.com to http://mydomain.com/m for example. So whatever is done here shouldn't prevent that from happening.

If it helps, the site is hosted on Rackspace Cloud Sites

Anybody know how to do this?

Thanks!

Edit

I tried this:

#http to https
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [L,R=301]

#subdomain 1
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub1.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/sub1/$1 [R=301,L]

It seems to work for the most part, except that when I type in a subfolder it doesn't seem to work. For example, if I type this into the address bar:

mydomain.com/temp

It resolves to:

http://mydomain.com/temp/

Edit 2

Well I've made some progress. So far I have www redirecting to non-www, and subdomains working (though the address bar does change - I guess that's acceptable). What seems to mess it up is if I put any sort of forced-https in there. It seems to conflict with the www to non-www block. I can have one or the other. Is there a way to make those two parts work together?

Also, the WordPress site Address and WordPress Address are both set to https://mydomain.com

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# Redirect from www to non-www location
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]

# Redirect http to https
# THIS DOES NOT WORK - causes redirect loop
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

# subdomain
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub1.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/sub1/$1 [R=301,L]

RewriteBase /
RewriteCond %{HTTP_HOST} ^sub2.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/sub2/$1 [R=301,L]

Conclusion

For most web hosts, anubhava's solution would work just fine. However, the key here turned out to be Rackspace, I believe. They have their own preferred methods that I found by searching through their knowledge base. Here's the final htaccess file that works great. All www urls get sent to non-www urls, http gets sent to https, and subdomains redirect to their proper subdirectories, without messing up WordPress:

RewriteEngine On
RewriteBase /

#subdomain 1
RewriteCond %{HTTP_HOST} ^sub1.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/sub1/$1 [R=301,L]

#subdomain 2
RewriteCond %{HTTP_HOST} ^sub2.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/sub2/$1 [R=301,L]

#get rid of www, works with rackspace cloud sites
RewriteCond %{HTTP_HOST} ^www.mydomain.com [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]

#force https, works with rackspace cloud sites
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [R,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Thanks so much for all your assistance with this.

ps2goat
  • 8,067
  • 1
  • 35
  • 68
Typel
  • 1,109
  • 1
  • 11
  • 34
  • jbalyo, as much as I appreciate you giving @anubhava credit, you should move your 'conclusion' to a separate answer and mark it as the answer. I completely skipped over your question to read the solved answer, but reading all of the comments to that answer led me back up to here. – ps2goat Mar 04 '14 at 06:47
  • instant +1 if you do ;) – ps2goat Mar 04 '14 at 06:56
  • @ps2goat: Fair point, I updated the answer with final outcome below. – anubhava Mar 04 '14 at 13:42

1 Answers1

3

You can have your .htaccess like this:

RewriteEngine On
RewriteBase /

#subdomain 1
RewriteCond %{HTTP_HOST} ^sub1.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/sub1/$1 [R=301,L]

#subdomain 2
RewriteCond %{HTTP_HOST} ^sub2.mydomain.com [NC]
RewriteRule ^(.*)$ http://mydomain.com/sub2/$1 [R=301,L]

#get rid of www, works with rackspace cloud sites
RewriteCond %{HTTP_HOST} ^www.mydomain.com [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]

#force https, works with rackspace cloud sites
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [R,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

PS: Note that the HTTPS server variable is not correctly implemented in a Rackspac Cloud Environment. The correct variable to look at for HTTPS in Rackspace Cloud is ENV:HTTPS.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I tried this, thanks - but it created a redirect loop and crashes the site. I'm not sure why... – Typel Jan 08 '14 at 19:26
  • I just tried that, still get a redirect loop. And now I can't access /wp-admin/ anymore. Hmm... – Typel Jan 08 '14 at 20:11
  • Okay the wp-admin thing must have been some weird DNS issue - maybe the domain is still propagating (we switched it about 12 hours ago). Yes, typing `https://mydomain.com/` works, but I'm trying to make it so that typing `http://mydomain.com/` will forward to the `https://` – Typel Jan 08 '14 at 20:21
  • Same result, sorry. Redirect loop. I tried removing the subdomain part and still have the same result. I'm going to update my question with my latest htaccess file. I've made a little progress – Typel Jan 08 '14 at 20:47
  • I tried that again, a redirect loop. This is strange. I almost wonder if WordPress might be trying to forward things differently, causing the loops. I did set the WP URLs to `https://mydomain.com` as you suggested. – Typel Jan 08 '14 at 21:55
  • It is an series of 21 `GET mydomain.com 301 Moved Permanently`s The first of which is Remote Port 80 and all subsequent requests are 443. I guess the browser is correct that it's a loop. But why would it try to redirect to https when it's already on https? – Typel Jan 08 '14 at 22:09
  • Yes, with the 3 http to https lines commented out, it loads fine. There is only one `GET mydomain.com 200 OK` and then it proceeds to get the other elements on the page. Of course, they are all port 80 – Typel Jan 08 '14 at 22:23
  • Is WordPress's normal behavior to forward everything to https if that is part of the wordpress URL? If so, maybe there is a conflict? – Typel Jan 08 '14 at 22:38
  • I figured it out! Rackspace has its own special methods. I only found it by searching through their help docs. Thanks so much for your help - your solution probably would work fine with any other host. – Typel Jan 08 '14 at 22:57
  • I thought the note about `ENV:HTTPS` was relevant at the top of the answer since the user was questioning about a rackspace cloud environment. That's how/why I found this question! – ps2goat Mar 04 '14 at 16:30