gpt4 book ai didi

mod-rewrite - 具有外部重定向和内部重写的 mod_rewrite

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:16 25 4
gpt4 key购买 nike

我正在尝试使用 mod_rewrite 重定向某些页面以使用 SSL。为此,我有:

RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^/login(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/contact-us(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/\..*$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC]
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_URI} ^/login(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/contact-us(\.php)?$ [NC]
RewriteRule ^(.+)\.php$ https://www.example.com/$1 [R=301,L]

这很好用,并且完全符合我的要求。

稍后在我的 .htacess 中我有一个:

RewriteRule ^members/(.+)/change-password$ members/.change-password.php?item=$1 [NC,QSA,L]

因此,如果 URL 显示为,例如:

http://www.example.com/members/foo-bar/change-password

在内部它将被处理为:

/members/.change-password.php?item=foo-bar

同样,这工作正常并且也在做我想要的。

我现在需要做的是将其包含在我原来的 SSL 重定向逻辑中,以确保任何更改密码请求都被重定向到相同的 URL,而不是通过 https。我试过:

RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^/login(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/contact-us(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/\..*$
RewriteCond %{REQUEST_URI} !^/members/.+/change-password [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC]
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_URI} ^/login(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/contact-us(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/members/.+/change-password [NC]
RewriteRule ^(.+)\.php$ https://www.example.com/$1 [R=301,L]

但这不起作用 - 我只是通过 http 传送页面。将 .+ 更改为 .* 似乎让我进入了永久重定向循环。

我猜这是因为内部重写,但无论我尝试什么,我似乎都无法解决它。

谁能给个建议?

谢谢,

亚当·M。

最佳答案

对 mod_rewrite 文档的进一步审查让我发现了一些我错过的关于它在 .htaccess 文件中的具体用法。基本上 [L] 标志实际上并不按照规范指示最后。相反,您需要使用 [END] 标志(http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l 指代)。

当然,这又导致我遇到另一个问题——我的托管服务提供商没有安装最新的 Apache 或 mod_rewrite,所以 [END] 标志触发了无处不在的 HTTP 500内部服务器错误。

那怎么办?好吧,我回到我原来的规则集,知道 [L] 没有按照我的预期进行,并立即发现了错误 - %{REQUEST_URI} 值已由内部重写更新:

RewriteRule ^members/(.+)/change-password$ members/.change-password.php?url-slug=$1 [NC,QSA,L]

因此改变我原来的重定向逻辑来排除这个解决了我的问题:

RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^/login(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/contact-us(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/\..*$
RewriteCond %{REQUEST_URI} !^/members/.+/change-password$ [NC]

RewriteCond %{REQUEST_URI} !^/members/\.change-password(\.php)? [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC]
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_URI} ^/login(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/contact-us(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/members/.+/change-password$ [NC]
RewriteRule ^(.+)(\.php)?$ https://www.example.com/$1 [R=301,L]

关于mod-rewrite - 具有外部重定向和内部重写的 mod_rewrite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10156093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com