【问题标题】:Redirect all urls from HTTP to HTTPS (with seo urls)将所有 url 从 HTTP 重定向到 HTTPS(使用 seo url)
【发布时间】:2017-04-20 14:08:12
【问题描述】:

我在 .htaccess 中使用的代码:

RewriteEngine On 
RewriteRule ^inc/.*$ index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !-l
RewriteCond %{REQUEST_FILENAME} !\.(ico|css|png|jpg|gif|js)$ [NC]
RewriteRule ^(.*)$ index.php [QSA,L]

我想重写它以将所有链接更改为 https://

当我添加时

RewriteEngine On 
RewriteRule ^inc/.*$ index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !-l
RewriteCond %{REQUEST_FILENAME} !\.(ico|css|png|jpg|gif|js)$ [NC]
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteCond %{HTTP_HOST} ^localhost [NC] 
RewriteRule ^(.*)$ https://localhost/$1 [L,R=301]

给我错误,重定向太多。

或者当尝试改变时

RewriteEngine On 
RewriteRule ^inc/.*$ index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !-l
RewriteCond %{REQUEST_FILENAME} !\.(ico|css|png|jpg|gif|js)$ [NC]
RewriteRule ^(.*)$ https://localhost/ [QSA,L]

也有太多的重定向。

我也在使用 apache 和 nginx。 nginx代码(也许可以通过nginx来做?)

server {
    listen      127.0.0.1:80;
    server_name localhost;
    error_log  /var/log/httpd/domains/localhost.error.log error;

    location / {
        proxy_pass      http://127.0.0.1:8080;
        location ~* ^.+\.(jpeg|jpg|gif|bmp|ico|svg|tif|tiff|css|js|htm|html|ttf|otf|webp|woff|txt|csv|rtf|doc|docx|xls|xlsx|ppt|pptx|odf|odp|ods|odt|pdf|psd|ai|eot|eps|ps|zip|tar|tgz|gz|rar|bz2|7z|aac|m4a|mp3|mp4|ogg|wav|wma|3gp|avi|flv|m4v|mkv|mov|mpeg|mpg|wmv|exe|iso|dmg|swf)$ {
            root           /home/admin/web/localhost/public_html;
            access_log     /var/log/httpd/domains/localhost.log combined;
            access_log     /var/log/httpd/domains/localhost.bytes bytes;
            expires        4h;
            try_files      $uri @fallback;
        }
    }

    location /error/ {
        alias   /home/admin/web/localhost/document_errors/;
    }

    location @fallback {
        proxy_pass      http://127.0.0.1:8080;
    }

    location ~ /\.ht    {return 404;}
    location ~ /\.svn/  {return 404;}
    location ~ /\.git/  {return 404;}
    location ~ /\.hg/   {return 404;}
    location ~ /\.bzr/  {return 404;}

    include /home/admin/conf/web/nginx.localhost.conf*;
}

【问题讨论】:

    标签: .htaccess nginx


    【解决方案1】:

    对于 Apache 重写,您可以使用%{HTTPS} 来检查它是否是 HTTPS URL。

    RewriteCond %{HTTPS} off
    RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
    

    当然你也可以用 nginx 来做。

    由于默认情况下 http 访问端口 80 而 https 访问端口 443,您只需在端口 80 服务器块中设置重定向到 https url。

    类似的东西。

    server {
        listen 80;
        server_name example.com;
        return 301 https://$host$request_uri;
    }
    
    server {
        listen 443;
        ssl on;
        # The rest of your config
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-02
      • 2020-08-07
      • 2018-05-05
      • 2016-02-26
      • 2018-04-17
      相关资源
      最近更新 更多