HELP
HELP

Reputation: 14575

Mod Rewrite 2 Part Question

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

Upvotes: 0

Views: 243

Answers (3)

anon
anon

Reputation:

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.

Upvotes: 2

Rob Williams
Rob Williams

Reputation: 1470

RewriteRule sitemap.php sitemap.xml

That should do it.

Upvotes: 2

gutch
gutch

Reputation: 7139

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.

Upvotes: 0

Related Questions