Redirect to HTTPS with .htaccess with a specific domain

匿名 (未验证) 提交于 2019-12-03 02:06:01

问题:

I currently have 2 domains that access to the same folder on my server: metrikstudios.com and ziced.com.

I want the users that enter through http://metrikstudios.com be redirected to https://metrikstudios.com and the users that enter through http://ziced.com don't be redirected to https://ziced.com.

I currently have this on my .htaccess

RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

Thanks

回答1:

You could simply add another RewriteCond to check if the host is metrikstudios.com

RewriteCond %{HTTP_HOST} ^metrikstudios\.com [NC] 

and it should look like this:

RewriteEngine On RewriteCond %{HTTP_HOST} ^metrikstudios\.com [NC] RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} 


回答2:

Linux & cPanel Linux-based accounts use

.htaccess

files to handle redirection.

Note: If you need to create a .htaccess file, you can use your control panel's file manager (Web & Classic / cPanel).

Using the following code in your .htaccess file automatically redirects visitors to the HTTPS version of your site:

RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] If you have an existing .htaccess file: 

Do not duplicate RewriteEngine On. Make sure the lines beginning RewriteCond and RewriteRule immediately follow the already-existing RewriteEngine On.

Windows & Plesk

Windows-based accounts use web.config files to handle redirection.

Note: If you need to create a web.config file, you can use your control panel's file manager (Web & Classic / Plesk).

Using the following code in your web.config file automatically redirects visitors to the HTTPS version of your site:

If you have an existing web.config file:

Ensure you have sections (i.e. opening and closing tags) for: system.webServer (which contains rewrite) rewrite (which contains rules) rules (which contains one or more rule sections) Insert any of those sections that do not exist. Insert the entire rule section, including match, conditions, and action, inside the rules section. Note: You're inserting the rule (without an 's') inside the rules (with an 's') section.



回答3:

The accepted solution above only redirects a non-www domain from http to https .

If you want to redirect both www and non-www versions of your domain to ssl put the following RewriteCond right above your http to https Rule or before RewriteCond %{HTTPS} off line :

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC] 

Here is the complete Rule to redirect a specific domain to https.

RewriteEngine on  # first we will check the host header #if it's wwwexample.com or example.com RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC] # now we will check the https header # if https is off (Is non-ssl) RewriteCond %{HTTPS} off #redirect the requeste to https RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301] 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!