mod_rewrite 对 URL 中的子字符串进行修改

发布于 2024-09-12 13:04:50 字数 1080 浏览 2 评论 0原文

我使用 mod_rewrite 在我的网站上强制使用 HTTPS,但我想将 URL 中包含子字符串 com_bookmangement 的任何 URL 更改为 HTTP。

所以

http://www.example.com/index.php?option=com_content&view=article&id=85&Itemid=140

将被定向到

https://www.example.com/index.php??option=com_content&view=article&id=85&Itemid=140

https://www.example.com/index.php?option=com_bookmanagement&controller=localbooks&Itemid=216

将被定向到

http://www.example.com/index.php?option=com_bookmanagement&controller=localbooks&Itemid=216

我已经尝试过但没有成功:

#rewrite everything apart from com_bookmanagement to HTTPS
RewriteCond %{SERVER_PORT} !443
RewriteCond %{REQUEST_URI} !com_bookmanagement=
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

#rewrite vouchers views to http
#RewriteCond %{server_port} 443
RewriteCond %{REQUEST_URI} ^com_bookmanagement=
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]

有什么想法吗?

I'm forcing HTTPS on my site using mod_rewrite but I want to change this to HTTP for any URL with the substring com_bookmangement in the URL.

So

http://www.example.com/index.php?option=com_content&view=article&id=85&Itemid=140

will be directed to

https://www.example.com/index.php??option=com_content&view=article&id=85&Itemid=140

BUT

https://www.example.com/index.php?option=com_bookmanagement&controller=localbooks&Itemid=216

will be directed to

http://www.example.com/index.php?option=com_bookmanagement&controller=localbooks&Itemid=216

I've tried this without success:

#rewrite everything apart from com_bookmanagement to HTTPS
RewriteCond %{SERVER_PORT} !443
RewriteCond %{REQUEST_URI} !com_bookmanagement=
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

#rewrite vouchers views to http
#RewriteCond %{server_port} 443
RewriteCond %{REQUEST_URI} ^com_bookmanagement=
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]

Any ideas?

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

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

发布评论

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

评论(2

绝不服输 2024-09-19 13:04:50

如果您使用 ^com_bookmanagement= ,则仅当 com_bookmanagement= 出现在字符串开头时才会匹配。尝试不使用 ^=。或者我会用什么:

#rewrite everything apart from com_bookmanagement to HTTPS
RewriteCond %{HTTPS} !=on
RewriteCond %{QUERY_STRING} !(^|&)option=com_bookmanagement(&|$)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#rewrite vouchers views to http
RewriteCond %{HTTPS} !=off
RewriteCond %{QUERY_STRING} (^|&)option=com_bookmanagement(&|$)
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

If you use ^com_bookmanagement= it will only match if com_bookmanagement= appears at the start of the string. Try it without the ^ and =. Or what I would use:

#rewrite everything apart from com_bookmanagement to HTTPS
RewriteCond %{HTTPS} !=on
RewriteCond %{QUERY_STRING} !(^|&)option=com_bookmanagement(&|$)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#rewrite vouchers views to http
RewriteCond %{HTTPS} !=off
RewriteCond %{QUERY_STRING} (^|&)option=com_bookmanagement(&|$)
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
暮年慕年 2024-09-19 13:04:50

使用 QUERY_STRING 而不是 REQUEST_URI 进行条件:

RewriteCond %{SERVER_PORT} !443
RewriteCond %{QUERY_STRING} !com_bookmanagement
RewriteRule ^(.*)$ https://www.mysite.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} com_bookmanagement
RewriteRule ^/(.*)$ http://www.mysite.com/$1 [R=301,L]

有关使用查询字符串重写 URI 的更多信息 此处

Condition on QUERY_STRING instead of REQUEST_URI:

RewriteCond %{SERVER_PORT} !443
RewriteCond %{QUERY_STRING} !com_bookmanagement
RewriteRule ^(.*)$ https://www.mysite.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} com_bookmanagement
RewriteRule ^/(.*)$ http://www.mysite.com/$1 [R=301,L]

More on rewriting URIs with query strings here.

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