使用外部重定向和内部重写的mod_rewrite [英] mod_rewrite with external redirect and internal rewrite

查看:91
本文介绍了使用外部重定向和内部重写的mod_rewrite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use mod_rewrite to redirect certain pages to use SSL. For that I have:

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]

这很好用,并且完全按照我的意愿做.

This works fine, and does exactly what I want it to do.

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

Later in my .htacess I have a:

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

因此,如果出现一个URL,例如:

So if a URL appears as, for example:

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

内部将其处理为:

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

同样,它可以正常工作,并且也可以满足我的要求.

Again, this works fine and is doing what I want it too.

我现在需要做的是将其包含在我的原始SSL重定向逻辑中,以确保将所有更改密码请求重定向到同一URL,但通过https重定向.我尝试过:

What I now need to do is include this in my original SSL redirect logic to ensure that any change password requests are redirected to the same URL but over https instead. I've tried:

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发送网页.将.+更改为.*似乎使我进入了永久重定向循环.

But this doesn't work - I just get the page delivered over http. Changing the .+ to .* appears to put me into a permanent redirect loop.

我猜这是由于内部重写而引起的,但是无论我尝试什么,我似乎都无法解决它.

I'm guessing this is because of the internal rewrite but no matter what I try I can't seem to resolve it.

任何人都可以请教吗?

谢谢

亚当M.

推荐答案

对mod_rewrite文档的进一步审查使我有些想念它在.htaccess文件中的用法.基本上,[L]标志实际上并没有按照规范指示最后一次.相反,您需要使用[END]标志( http://httpd. apache.org/docs/current/rewrite/flags.html#flag_l 引用).

A further review of the mod_rewrite documentation led me to a bit I'd missed specific to its usage in .htaccess files. Basically the [L] flag doesn't actually indicate last as per the norm. Instead you need to use the [END] flag (http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l refers).

当然,这又导致我遇到另一个问题-我的托管服务提供商没有Apache或mod_rewrite的最新安装,因此[END]标志触发了普遍存在的HTTP 500 Internal Server Error.

Of course that then led me to another issue - my hosting provider doesn't have an up-to-date installation of either Apache or mod_rewrite so the [END] flag triggered the ubiqitous HTTP 500 Internal Server Error.

那该怎么办?好吧,我回到原来的规则集,了解到[L]并没有达到我的期望,并立即发现了错误-%{REQUEST_URI}值已通过内部重写进行了更新:

So what to do? Well I went back to my original ruleset with the knowledge that [L] wasn't doing what I was expecting and spotted the error straight away - the %{REQUEST_URI} value had been updated by the internal rewrite:

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

因此更改我的原始重定向逻辑以排除此问题解决了我的问题:

Therefore changing my original redirection logic to exclude this resolved my issue:

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]

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆