【问题标题】:Apache Redirect to www and https with cgi-binApache 使用 cgi-bin 重定向到 www 和 https
【发布时间】:2016-09-20 20:40:31
【问题描述】:

我正在尝试将 apache 配置为重定向到我们服务器上的 www 和 https,我们使用一个 ecom 包,该包使用驻留在 cgi-bin 文件夹中的 vlink 脚本。

将/cgi-bin/scriptname改写为/的指令是:-

RewriteEngine   on

RewriteRule     ^$      /cgi-bin/scriptname/  [L,NS,PT]
RewriteRule     ^/$      /cgi-bin/scriptname/  [L,NS,PT]
RewriteRule     ^/index\.html$      /cgi-bin/scriptname/  [L,NS,PT]

RewriteCond     /home/scriptname/www/html/%{REQUEST_FILENAME} -f      [OR]
RewriteCond     /home/scriptname/www/html/%{REQUEST_FILENAME} -d
RewriteRule     ^(.+)$  -  [L,NS]

RewriteCond     /%{REQUEST_FILENAME}    !^.*/cgi-bin/scriptname.*$
RewriteRule     ^(.+)$  /cgi-bin/scriptname/$1  [L,NS,PT]

我在 apache conf 的 :80 和 :443 vhost 部分都有这个。

现在我想确保所有 url 都重定向到 https 和 www,所以我将其添加到 .htaccess 中

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

这似乎有效,但是当我查看使用 wget 发生的情况时,我看到多个 301 永久移动,有时 3 或 4 个,具体取决于 url,有些是指 /cgi-bin/scriptname

这是正确的吗,我在想我应该只看到一个 301 移动,然后是一个 200?

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite


    【解决方案1】:

    我已经提出了以下解决方案,并且欢迎 cmets,它似乎可以在我拥有的端口 80 虚拟主机部分中使用

    #not www with cgi-bin    
    RewriteCond     /%{REQUEST_FILENAME}    ^.*/cgi-bin/scriptname.*$
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^/cgi-bin/scriptname/(.*) https://www.example.com/$1 [L,R=301]  
    
    #cgi-bin
    RewriteCond     /%{REQUEST_FILENAME}    ^.*/cgi-bin/scriptname.*$
    RewriteRule ^/cgi-bin/scriptname/(.*) https://www.example.com/$1 [L,R=301]
    
    #not www
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    #not https
    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    在端口 443 虚拟主机部分

    #not www with cgi-bin    
    RewriteCond     /%{REQUEST_FILENAME}    ^.*/cgi-bin/scriptname.*$
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^/cgi-bin/scriptname/(.*) https://www.example.com/$1 [L,R=301]  
    
    #cgi-bin
    RewriteCond     /%{REQUEST_FILENAME}    ^.*/cgi-bin/scriptname.*$
    RewriteRule ^/cgi-bin/scriptname/(.*) https://www.example.com/$1 [L,R=301]
    
    #not www
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    这似乎只为每个 url 组合做 1 301 次重定向。

    【讨论】:

      猜你喜欢
      • 2014-10-28
      • 2018-11-16
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 2020-05-22
      相关资源
      最近更新 更多