我在将 url 查询参数重写 (fancy-url) 与 .htaccess ssl 重定向相结合时遇到问题。
我的 .htaccess 文件当前是:
Options +FollowSymLinks
Options -Indexes
ServerSignature Off
RewriteEngine on
RewriteBase /
# in https: process secure.html in https
RewriteCond %{server_port} =443
RewriteCond $1 ^secure$ [NC]
RewriteRule ^(.+).html$ index.php?page=$1 [QSA,L]
# in https: force all other pages to http
RewriteCond %{server_port} =443
RewriteCond $1 !^secure$ [NC]
RewriteRule ^(.+).html$ http://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]
# in http: force secure.html to https
RewriteCond %{server_port} !=443
RewriteCond $1 ^secure$ [NC]
RewriteRule ^(.+).html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]
# in http: process other pages as http
RewriteCond %{server_port} !=443
RewriteCond $1 !^secure$ [NC]
RewriteRule ^(.+).html$ index.php?page=$1 [QSA,L]
fancy-url 重写工作正常,但到/从 https 的重定向根本不起作用。
如果我替换包含
的两行RewriteRule ^(.+).html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]
与
RewriteRule ^(.+).html$ https://%{HTTP_HOST}/index.php?page=$1 [QSA,L]
然后 https 重定向工作正常,但 fancy-url 重写不起作用。
是否可以将这两者结合起来?
编辑:
期望的结果是:
1. http://domain.com/secure.html is rewritten to https://domain.com/index.php?page=secure
2. http://domain.com/foo.html is rewritten to http://domain.com/index.php?page=foo
3. https://domain.com/secure.html is rewritten to https://domain.com/index.php?page=secure
4. https://domain.com/foo.html is rewritten to http://domain.com/index.php?page=foo
(我不得不将它们放在一个代码块中,因为新用户不允许超过 1 个链接)
因此 secure.html 始终是 https,而 foo.html(所有其他页面)始终是 http。
解决方案:
感谢 Gumbo,解决方案是:
Options +FollowSymLinks
Options -Indexes
ServerSignature Off
RewriteEngine on
RewriteBase /
# in https: force all other pages to http
RewriteCond %{server_port} =443
RewriteCond $1 !^secure$ [NC]
RewriteRule ^(.+)\.html$ http://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]
# in http: force secure.html to https
RewriteCond %{server_port} !=443
RewriteCond $1 ^secure$ [NC]
RewriteRule ^(.+)\.html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]
# in https: process secure.html in https
RewriteCond %{server_port} =443
RewriteCond $1 ^secure$ [NC]
RewriteRule ^(.+)\.html$ index.php?page=$1 [QSA,L]
# in http: process other pages as http
RewriteCond %{server_port} !=443
RewriteCond $1 !^secure$ [NC]
RewriteRule ^(.+)\.html$ index.php?page=$1 [QSA,L]
最佳答案
你需要把那些导致外部重定向的规则放在那些只导致内部重定向的规则之前。所以:
# in https: force all other pages to http
RewriteCond %{SERVER_PORT} =443
RewriteCond $1 !^secure$ [NC]
RewriteRule ^(.+)\.html$ http://%{HTTP_HOST}%{REQUEST_URI} [QSA,L]
# in http: force secure.html to https
RewriteCond %{SERVER_PORT} !=443
RewriteCond $1 ^secure$ [NC]
RewriteRule ^(.+)\.html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L]
# in https: process secure.html in https
RewriteCond %{SERVER_PORT} =443
RewriteCond $1 ^secure$ [NC]
RewriteRule ^(.+)\.html$ index.php?page=$1 [QSA,L]
# in http: process other pages as http
RewriteCond %{SERVER_PORT} !=443
RewriteCond $1 !^secure$ [NC]
RewriteRule ^(.+)\.html$ index.php?page=$1 [QSA,L]
关于.htaccess url 使用 ssl 重定向重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2443240/