Mod Rewrite 2部分问题 [英] Mod Rewrite 2 Part Question
问题描述
我想知道如何使用mod_rewrite重写以下URL?并有在线教程解释了针对假人的mod_rewrite吗?
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?
如何重写此URL.
http://www.example.com/sitemap.php
到
http://www.example.com/sitemap.xml
推荐答案
为此,您有两种选择,具体取决于您希望它如何工作.
You have a couple options for this, depending on how you want it to work.
将一个文件重定向到另一个文件的最简单方法是简单地使用Redirect
指令:
The simplest way to redirect one file to another is to simply use the Redirect
directive:
Redirect /sitemap.php http://www.example.com/sitemap.xml
默认情况下,这将执行302重定向,但是您可以通过添加状态代码作为第一个参数来将其更改为301,即:
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.:
Redirect 301 /sitemap.php http://www.example.com/sitemap.xml
在两种情况下,这都将导致返回浏览器的往返,因此地址栏将更改为显示新的sitemap.xml
文件名.如果您不想这样做,可以使用RewriteRule
指令:
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 the RewriteRule
directive:
RewriteRule ^sitemap.php$ /sitemap.xml [L]
请注意,这是从所谓的按目录上下文"编写规则的方式,这意味着从.htaccess
文件或<Directory>
块中写入规则.如果您是从主配置中编写代码,则需要一个斜杠(即^/sitemap.php$
)以表明您的意思是该规则要与文档根目录相匹配.
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.
这篇关于Mod Rewrite 2部分问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!