使用 mod_rewrite 和 symfony 保护 URL

发布于 2024-11-04 11:42:39 字数 1512 浏览 2 评论 0原文

我有几个页面需要通过 mod_rewrite 进行保护,并且代码基于 mvc 架构

让我说我有一个页面登录,其 url 是 http://www.example.com/login 需要重定向到 https:// /www.example.com/login

如果除了所需的安全 url 之外的任何 url 使用 https,我们需要将其更改为 http 例如 https://www.example.com/sitemap 必须重定向到 http://www.example.com/sitemap

我在 .htaccess 中使用以下代码

  RewriteCond %{SERVER_PORT} !^443$
  RewriteRule ^login$ https://%{HTTP_HOST}/login [R=301,L]

  RewriteCond %{SERVER_PORT} ^443$
  RewriteCond %{REQUEST_URI} !^/login$
  RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

  # we check if the .html version is here (caching)
  RewriteRule ^$ index.html [QSA]
  RewriteRule ^([^.]+)$ $1.html [QSA]
  RewriteCond %{REQUEST_FILENAME} !-f

  # no, so we redirect to our front web controller
  RewriteRule ^(.*)$ index.php [QSA,L]

我遇到的问题是,它在它的位置循环说“服务器正在以永远不会完成的方式重定向对此地址的请求”。

您能否帮我解决仅以下网址受到保护而其他网址不受保护的解决方案。我需要一个带有 .htaccess 的解决方案,而不是任何 symfony 插件。

https://www.example.com/login

https://www.example.com/account

https ://www.example.com/register

谢谢尼扎姆

I have few pages which need to be secured through mod_rewrite and the code is based on mvc architecture

Let me say I have a page login its url is http://www.example.com/login it needs to be redirected to https://www.example.com/login

If any url other than desired secured url uses https we need to change it to http for example
https://www.example.com/sitemap must be redirected to http://www.example.com/sitemap

I am using the following code in .htaccess

  RewriteCond %{SERVER_PORT} !^443$
  RewriteRule ^login$ https://%{HTTP_HOST}/login [R=301,L]

  RewriteCond %{SERVER_PORT} ^443$
  RewriteCond %{REQUEST_URI} !^/login$
  RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

  # we check if the .html version is here (caching)
  RewriteRule ^$ index.html [QSA]
  RewriteRule ^([^.]+)$ $1.html [QSA]
  RewriteCond %{REQUEST_FILENAME} !-f

  # no, so we redirect to our front web controller
  RewriteRule ^(.*)$ index.php [QSA,L]

The problem I get is, it gets looped where it says "server is redirecting the request for this address in a way that will never complete".

Can you please help me out with the solution where only the following urls are secured and others are not. I need a solution with .htaccess and not with any symfony plugin.

https://www.example.com/login

https://www.example.com/account

https://www.example.com/register

Thanks

Nizam

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

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

发布评论

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

评论(2

傾城如夢未必闌珊 2024-11-11 11:42:39

我认为问题是第五行正则表达式中不需要的斜杠。试试这个

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^login$ https://%{HTTP_HOST}/login [R=301,L]

RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^login$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

I think the problem is an unwanted slash in your fifth line's regex. Try this

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^login$ https://%{HTTP_HOST}/login [R=301,L]

RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^login$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
栖迟 2024-11-11 11:42:39

这对我来说非常有效,

  # SSL here
  RewriteCond %{SERVER_PORT} !^443$
  RewriteRule ^/?(login|account|register|mypage)(.*) https://%{HTTP_HOST}/$1$2 [R,L]

  # not anywhere else
  RewriteCond %{SERVER_PORT} !^80$
  RewriteCond %{REQUEST_URI} !^/?(login|account|register|mypage)(.*)
  RewriteCond %{REQUEST_URI} !^/?index\.php$
  RewriteRule .? http://%{HTTP_HOST}%{REQUEST_URI} [R,L]

感谢 Prem、Amar、Harry 提供的解决方案。

This worked perfectly for me

  # SSL here
  RewriteCond %{SERVER_PORT} !^443$
  RewriteRule ^/?(login|account|register|mypage)(.*) https://%{HTTP_HOST}/$1$2 [R,L]

  # not anywhere else
  RewriteCond %{SERVER_PORT} !^80$
  RewriteCond %{REQUEST_URI} !^/?(login|account|register|mypage)(.*)
  RewriteCond %{REQUEST_URI} !^/?index\.php$
  RewriteRule .? http://%{HTTP_HOST}%{REQUEST_URI} [R,L]

Thanks Prem, Amar, Harry for the solution.

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