I was wondering how can I rewrite the following URL below using mod_rewrite? And is there a online tutorial that explains mod_rewri开发者_运维知识库te for dummies?
How can I rewrite this URL.
http://www.example.com/sitemap.php
to
http://www.example.com/sitemap.xml
RewriteRule sitemap.php sitemap.xml
That should do it.
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.
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.
精彩评论