htaccess-使用PHP GET变量重写和重定向URL [英] Htaccess - Rewrite and Redirect URL with PHP GET variable

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

问题描述

我正在尝试重写和重定向此URL:

I am trying to both rewrite and redirect this URL:

https://www.domain.com/video-lessons-page.php?item=stocks

转到此URL:

https://www.domain.com/stocks-video-lessons

这是我目前正在使用的代码:

Here is the code I'm currently working with:

RewriteCond %{QUERY_STRING} ^item=([^&]+)
RewriteRule ^/?video-lessons-page\.php$ https://www.domain.com/%1-video-lessons? [R=301,L]
RewriteRule ^(stocks|finance|accounting)-video-lessons$ video-lessons-page.php?item=$1 [NC,L]

不幸的是,使用上面的代码,我遇到了问题.前两行本身非常有用,因为它会将/video-lessons-page.php?item=stocks 重定向到/stocks-video-lessons .

Unfortunately with the above code I'm having issues. The first two lines work great by themselves in that it redirects /video-lessons-page.php?item=stocks to /stocks-video-lessons.

第三行本身也可以很好地与/stocks-video-lessons URL一起使用GET变量和页面代码正常工作.

And the third line also works great by itself to where the /stocks-video-lessons URL functions correctly with the GET variable and the page's code.

但是将它们放在一起是有问题的.目前,我收到诸如"www.domain.com重定向您太​​多次"之类的错误.等.有什么想法吗?

But putting them together is problematic. Right now I'm getting errors like "www.domain.com redirected you too many times." etc. Any ideas?

推荐答案

请澄清一下,大概您已经将所有内部URL更改为/stocks-video-lessons 形式,并说明了原因外部重定向是因为丑陋的" URL已经被索引并在外部链接了?

Just to clarify, presumably you have already changed all your internal URLs to the form /stocks-video-lessons and the reason for the external redirect is because the "ugly" URLs have already been indexed and linked externally?

您已经发现,由于这两个规则相互冲突,并且不断地从一个重定向到另一个,因此您将获得重定向循环.

As you have found, you are getting a redirect loop because these two rules conflict and it's constantly redirecting/rewriting from one to the other.

仅在初始请求包含URL /video-lessons-page.php 而不是重写的URL时,您需要 redirect 这就是这里发生的事情).您可以通过检查 THE_REQUEST 服务器变量(包含浏览器发送的完整HTTP请求)来检查初始请求.

You need to redirect when only the initial request contains the URL /video-lessons-page.php, not the rewritten URL (which is what's happening here). You can check the initial request by checking against THE_REQUEST server variable (which contains the full HTTP request as sent by the browser).

类似以下内容:

RewriteCond %{THE_REQUEST} /video-lessons-page\.php?item=(\w+)
RewriteRule ^video-lessons-page\.php$ https://www.example.com/%1-video-lessons? [R=301,L]

RewriteRule ^(stocks|finance|accounting)-video-lessons$ video-lessons-page.php?item=$1 [L]

请注意,在进行测试之前,您必须清除浏览器缓存,因为301(永久)重定向将被浏览器缓存.(通常更容易使用302重定向进行测试.)如果您特别需要 不区分大小写的匹配项,则仅使用 NC 标志.

Note you must clear your browser cache before testing, since 301 (permanent) redirects will be cached by the browser. (Often easier to test with 302 redirects.) Only use the NC flag if you specifically need a case-insensitive match.

更新:我已将查询字符串参数值的正则表达式从( [^&] + )更改为限制性更强的(\ w + ). \ w 是单词字符的简写字符类(自然会排除& 并严格排除空格).由于 THE_REQUEST 包含以下形式的字符串:

UPDATE: I've changed the regex on the query string parameter value, from ([^&]+) to the slightly more restrictive (\w+). \w being the shorthand character class for a word character (which naturally excludes & and crucially spaces). Since THE_REQUEST contains a string of the form:

GET /video-lessons-page.php?item=stocks HTTP/1.1

...我们需要停止捕获之前的空间.先前的([[^&] +)模式将捕获到字符串末尾的所有内容,即.股票HTTP/1.1".因此,我会期望 404,因为将发生无效的重定向,但内部重写将失败.(?)

...we need to stop capturing before the space. The previous pattern ([^&]+) would have captured everything to the end of the string ie. "stocks HTTP/1.1". So, I would have expected a 404, since an invalid redirect would have occurred but the internal rewrite would have failed. (?)

这篇关于htaccess-使用PHP GET变量重写和重定向URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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