使用 mod_rewrite 和 symfony 保护 URL
我有几个页面需要通过 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/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/account
https://www.example.com/register
Thanks
Nizam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题是第五行正则表达式中不需要的斜杠。试试这个
I think the problem is an unwanted slash in your fifth line's regex. Try this
这对我来说非常有效,
感谢 Prem、Amar、Harry 提供的解决方案。
This worked perfectly for me
Thanks Prem, Amar, Harry for the solution.