Apache使用cgi-bin重定向到www和https

时间:2016-09-20 20:40:32

标签: apache .htaccess redirect mod-rewrite

我正在尝试将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部分都有这个。

现在我想确保所有网址都重定向到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?

1 个答案:

答案 0 :(得分:0)

我已经提出了以下解决方案,并欢迎评论,它似乎工作,在端口80 vhost部分我有

#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 vhost部分

#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]

这似乎只对每个网址组合进行1 301重定向。