简体   繁体   English

如何使用.htaccess重写到具有php标头重定向的页面?

[英]How can I use a .htaccess rewrite to a page that has a php header redirect?

this is my first post so go easy on me. 这是我的第一篇文章,所以对我轻松一点。

Basically I am doing some rewrites in my htaccess file to change my made up search friendly URLs into actual URLs, and for the most part they are working. 基本上,我正在htaccess文件中进行一些重写,以将组成的对搜索友好的URL更改为实际的URL,并且大多数情况下它们都在工作。 For instance this: 例如:

http://www.negativeworld.org/7849/news/nintendo-download-for-may-24-2012 http://www.negativeworld.org/7849/news/nintendo-download-for-may-24-2012

Will turn into this: 会变成这样:

http://www.negativeworld.org/article.php?id=7849 http://www.negativeworld.org/article.php?id=7849

Just fine... IF that article exists. 很好...如果该文章存在。 If the article doesn't exist, the php code uses this: 如果该文章不存在,则php代码使用以下代码:

header("Location: boarderror.php");
exit;

To bring the user to boarderror.php. 将用户带到boarderror.php。 This works fine if it the user gets there directly on article.php and the id is bad, but when I am trying to do the htaccess redirect from a search friendly url and the id is bad, the htaccess redirect just hangs for awhile before giving me this message: "The page isn't redirecting properly". 如果用户直接在article.php上到达那里并且id不好,这会很好,但是当我尝试通过搜索友好的URL进行htaccess重定向并且id不好时,htaccess重定向会在挂起之前暂停一会儿出现以下消息:“页面未正确重定向”。

What I want is for it to go to my boarderror.php page when there is a bad id. 我想要的是在存在错误的ID时转到我的boarderror.php页面。 So basically I want my htaccess page to take a server friendly URL, switch to the true URL, and well... just let go at that point, and the PHP will take it from there. 因此,基本上,我希望htaccess页采用服务器友好的URL,切换到真实URL,然后...放开这一点,PHP将从那里开始。 Here is my htaccess line that does the switch: 这是我进行切换的htaccess行:

RewriteRule ^([0-9]+)/(news|review|editorial|podcast)/(.*)$ /article.php?id=$1 [L]

What am I doing wrong? 我究竟做错了什么? (BTW I realize that if I set up all of my search friendly URLs correctly there should never be a bad id anyway, but I want to be on the safe side...) (顺便说一句,我意识到,如果我正确地设置了所有对搜索友好的网址,则无论如何都不会出现错误的ID,但是我希望安全起见……)

Your thoughts aren't wrong. 你的想法没错。 For a wrong ID there is a double redirection which is OK. 对于错误的ID,可以进行双重重定向。 The problem is how the second redirection happens. 问题是第二重定向如何发生。 Try 尝试

header("Location: http://www.negativeworld.org/boarderror.php");

or 要么

header("Location: /boarderror.php");

With your redirection the browser is trying http://www.negativeworld.org/9999/news/boarderror.php (being 9999 the wrong ID) which falls in an endless redirection loop that the browser cuts after 10 tries. 通过重定向,浏览器正在尝试http://www.negativeworld.org/9999/news/boarderror.php (错误的ID为9999),这陷入了无尽的重定向循环,浏览器在尝试10次后便被剪切。

The redirect rule is fine, the issue is in your header function call. 重定向规则很好,问题出在您的标头函数调用中。 When you only provide a file name, the header redirect will send the user to the file in the same folder, much like creating an html link using only the filename. 仅提供文件名时,标头重定向会将用户发送到同一文件夹中的文件,就像仅使用文件名创建html链接一样。

Let's say i try to load http://www.negativeworld.org/99999/news/nintendo-download-for-may-24-2012 and that id is invalid. 假设我尝试加载http://www.negativeworld.org/99999/news/nintendo-download-for-may-24-2012并且该ID无效。 In this case it would send send the user to http://www.negativeworld.org/99999/news/boarderror.php which triggers the redirect again and gets stuck in an infinite loop (or would if the browser wasn't smart enough to stop requesting the same URL over and over again). 在这种情况下,它将把用户发送到http://www.negativeworld.org/99999/news/boarderror.php ,它再次触发重定向并陷入无限循环(或者如果浏览器不够智能,也会陷入这种情况)停止一遍又一遍地请求相同的网址)。

Per RFC 2616 the location header should provide an absolute URI, so you should do something like this: 根据RFC 2616 ,位置标头应提供绝对URI,因此您应执行以下操作:

header("Location: http://www.negativeworld.org/boarderror.php");
exit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM