来自 mod_rewrite RewriteMap 程序的 301 重定向
我正在使用 mod_rewrite 的 RewriteMap 指令来处理 URL。我的 RewriteMap 程序是一个 PHP 脚本,一切运行正常。我能够将友好的 URL 映射到 PHP 程序 + ID。无论如何,我想要做的是返回某些 URL 的 301 重定向。例如,如果有人输入 URL:
http://www.example.com/directory1
然后我希望我的 RewriteMap 程序将 301 重定向发送到
http:// www.example.com/directory1/
(尾部斜杠)
然后将再次进入我的程序以映射到 PHP 脚本。我尝试在语句末尾添加 [R=301],但这只是挂起请求。这是我的脚本的基本逻辑:
if ($input_url == "/directory1") { echo "/directory1/ [R=301]\n"; // this doesn't work... just hangs } else if ($input_url == "/directory1/") { echo "/myprogram.php?id=1\n"; }
有什么想法吗?
I'm using mod_rewrite's RewriteMap directive to process URLs. My RewriteMap program is a PHP script and everything is running fine. I'm able to map friendly URLs to PHP program + ID. Anyway, what I want to do is return a 301 redirect for certain URLs. For example, if someone puts in the URL:
http://www.example.com/directory1
Then I want my RewriteMap program to send a 301 redirect to
http://www.example.com/directory1/
(trailing slash)
Which will then go into my program again to be mapped onto a PHP script. I tried adding [R=301] at the end of my statement, but this just hangs the request. Here's the basic logic of my script:
if ($input_url == "/directory1") { echo "/directory1/ [R=301]\n"; // this doesn't work... just hangs } else if ($input_url == "/directory1/") { echo "/myprogram.php?id=1\n"; }
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
RewriteCond 中的 -d 测试是专门为您的案例 Sridhar 设计的。它测试文件系统中是否存在目录。如果为 true,并且没有尾部斜杠,则您可以应用重定向。就像这样:
在这种情况下,您不需要 RewriteMap (prg)。
The -d test in RewriteCond is designed specifically for your case Sridhar. It tests if there is a directory present in the filesystem. If true, AND if there is no trailing slash, then you could apply the redirect. That would be like this:
You wouldn't need a RewriteMap (prg) in this case.
这是不可能的。但是您可以使用附加规则来重定向所有不带尾部斜杠的 URL 请求:
That’s not possible the way you want to do it. But you could use an additional rule to redirect all requests of URLs without a trailing slash: