Redirect to HTTPS with .htaccess with a specific domain

前端 未结 4 635

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://

相关标签:
4条回答
  • 2020-12-15 21:54

    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:

    <configuration>
    <system.webServer>
    <rewrite>
        <rules>
        <rule name="HTTP to HTTPS redirect" stopProcessing="true"> 
        <match url="(.*)" /> 
        <conditions> 
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        </conditions> 
        <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
    </rule>   
        </rules>
    </rewrite>
    </system.webServer>
    </configuration>
    

    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.

    0 讨论(0)
  • 2020-12-15 21:56

    Sometimes you might want to redirect only on live server and leave local settings for it as is. For example if on local machine you have registered local host named www.mysite.loc and set up local instance of project on this host.

    In this case this might help someone too:

    RewriteEngine On
    RewriteCond %{HTTPS} =off
    RewriteCond %{HTTP_HOST} !.loc$ [NC]
    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
    

    where !.loc$ - rule to ignore redirect to https if host ends with .loc.

    0 讨论(0)
  • 2020-12-15 22:05

    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 (url)
    #if it's www.example.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 request to https
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
    
    0 讨论(0)
  • 2020-12-15 22:13

    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}
    
    0 讨论(0)
提交回复
热议问题