.htaccess url 使用 ssl 重定向重写

标签 .htaccess mod-rewrite ssl url-rewriting

我在将 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/

相关文章:

php - .htaccess 删除 .php 并添加尾部斜杠

php - htaccess - 使用 php 和 mysql 重写 url

php - 使用 htaccess 和 godaddy 将 HTTP 重定向到 HTTPS

php - Apache:mod_rewrite 不指向一个地址

regex - 设置 410 - 特定 url 的 STATUS GONE

php - cURL 请求在服务器上工作正常但不会在我的本地主机上工作 :8080

.htaccess - .htaccess 上的 URL 重写

php - REQUEST_URI 不会被使用 APACHE RewriteRule 覆盖吗?

ruby-on-rails-3 - Rails SSL 仅在一个特定路径上

c# - 无法从 soap c# SoapHttpClientProtocol 创建 SSL/TLS 安全通道