Mod 重写 2 部分问题

发布于 2024-10-07 17:01:40 字数 221 浏览 0 评论 0原文

我想知道如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-10-14 17:01:40
RewriteRule sitemap.php sitemap.xml

应该可以做到这一点。

RewriteRule sitemap.php sitemap.xml

That should do it.

过期情话 2024-10-14 17:01:40

为此,您有几种选择,具体取决于您希望它如何工作。

将一个文件重定向到另一个文件的最简单方法是使用 Redirect 指令:

 Redirect /sitemap.php http://www.example.com/sitemap.xml

默认情况下,这将执行 302 重定向,但您可以通过添加状态代码作为第一个参数来将其更改为 301 ,即:

 Redirect 301 /sitemap.php http://www.example.com/sitemap.xml

在这两种情况下,这都会导致返回浏览器的往返,因此地址栏将更改以显示新的 sitemap.xml 文件名。如果您不希望这样,您可以使用 RewriteRule 指令:

 RewriteRule ^sitemap.php$ /sitemap.xml [L]

请注意,这是您从所谓的“每个目录上下文”编写规则的方式,这仅意味着该规则是从 .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:

 Redirect /sitemap.php http://www.example.com/sitemap.xml

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

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]

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.

伴我心暖 2024-10-14 17:01:40

我建议:

RewriteRule ^/sitemap.php$ /sitemap.xml

至于教程,您是否尝试过官方的 Apache mod_rewrite 简介
接下来,URL 重写指南中有很多有用的示例高级 URL 重写指南...它们可能很复杂,但确实有许多常见重写任务的示例。

I suggest:

RewriteRule ^/sitemap.php$ /sitemap.xml

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文