Mod 重写 2 部分问题
我想知道如何使用 mod_rewrite 重写下面的 URL?有没有在线教程可以为傻瓜解释 mod_rewrite ?
我怎样才能重写这个URL。
http://www.example.com/sitemap.php
到
http://www.example.com/sitemap.xml
I was wondering how can I rewrite the following URL below using mod_rewrite? And is there a online tutorial that explains mod_rewrite for dummies?
How can I rewrite this URL.
http://www.example.com/sitemap.php
to
http://www.example.com/sitemap.xml
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该可以做到这一点。
That should do it.
为此,您有几种选择,具体取决于您希望它如何工作。
将一个文件重定向到另一个文件的最简单方法是使用
Redirect
指令:默认情况下,这将执行 302 重定向,但您可以通过添加状态代码作为第一个参数来将其更改为 301 ,即:
在这两种情况下,这都会导致返回浏览器的往返,因此地址栏将更改以显示新的
sitemap.xml
文件名。如果您不希望这样,您可以使用RewriteRule
指令:请注意,这是您从所谓的“每个目录上下文”编写规则的方式,这仅意味着该规则是从
.htaccess
文件或
块中写入。如果您从主配置中编写它,那么您需要一个前导斜杠(即^/sitemap.php$
)来表明您的意思是从文档根匹配规则。You have a couple options for this, depending on how you want it to work.
The simplest way to redirect one file to another is to simply use the
Redirect
directive:This will do a 302 redirect by default, but you can change it to a 301 by adding the status code as a first parameter, i.e.:
In both cases, this will result in a round trip back to the browser, so the address bar will change to show the new
sitemap.xml
filename. If you don't want that, you can use theRewriteRule
directive:Note that this is how you would write the rule from what is called a "per-dir context" which just means that the rule is being written from within either a
.htaccess
file, or from a<Directory>
block. If you're writing it from your main config, then you would need a leading slash (i.e.^/sitemap.php$
) to show that you mean for the rule to match from the document root.我建议:
至于教程,您是否尝试过官方的 Apache mod_rewrite 简介?
接下来,URL 重写指南中有很多有用的示例高级 URL 重写指南...它们可能很复杂,但确实有许多常见重写任务的示例。
I suggest:
As for a tutorial, have you tried the official Apache mod_rewrite Introduction?
Following that, there are lots of useful examples in the URL Rewriting Guide and Advanced URL Rewriting Guide... they might be complex but it does have examples of many common rewrite tasks.