htaccess to redirect http:// and http://www to htt

2020-07-19 03:48发布

问题:

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.

回答1:

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.