Mod Rewrite 2 Part Question

后端 未结 3 593
無奈伤痛
無奈伤痛 2021-01-27 12:09

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

相关标签:
3条回答
  • 2021-01-27 12:27

    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.

    0 讨论(0)
  • 2021-01-27 12:34
    RewriteRule sitemap.php sitemap.xml
    

    That should do it.

    0 讨论(0)
  • 2021-01-27 12:38

    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.

    0 讨论(0)
提交回复
热议问题