.htaccess url 重写与 ssl 重定向

发布于 2024-08-25 11:16:51 字数 2551 浏览 4 评论 0原文

我在将 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 的重定向根本不起作用。

的 2 行,

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]

I'm having trouble combining a url query parameter rewrite (fancy-url) with a .htaccess ssl redirection.

My .htaccess file is currently:

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]

The fancy-url rewriting is working fine but the redirection to/from https isn't working at all.

If I replace the 2 lines containing

RewriteRule ^(.+).html$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,N]

with

RewriteRule ^(.+).html$ https://%{HTTP_HOST}/index.php?page=$1 [QSA,L]

then the https redirection works fine but the fancy-url rewriting doesn't work.

Is it possible to combine these two?

Edit:

The desired results are:

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

(I had to put them in a code block as more than 1 link is not allowed for new users)

So secure.html is always https while foo.html (all other pages) are always http.

Solution:

Thanks to Gumbo, the solution is:

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聊慰 2024-09-01 11:16:51

您需要将导致外部重定向的规则放在仅导致内部重定向的规则之前。所以:

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

You need to put those rule, that cause an external redirect, before those rules, that just cause an internal redirect. So:

# 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]
姐不稀罕 2024-09-01 11:16:51

这不是检测服务器端口的正确方法,它应该遵循与 http_host 相同的规则,分别使用 ^443$!^443 。您确实应该将 server_port 也大写,这是一个很好的做法。这是一个很好的小教程,可能会对您有所帮助出去。

That is not the correct way to detect what the server port is, it should follow the same rules as http_host using ^443$ and !^443 respectively. You really should capitalize server_port as well, it's good practice. Here is a good little tutorial that might help you out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文