1

我正在尝试使用 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 传递页面。更改.+to.*似乎让我进入了一个永久的重定向循环。

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

有人可以请教吗?

谢谢,

亚当 M。

4

1 回答 1

3

对 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]
于 2012-04-15T17:28:56.130 回答