RewriteEngine On mandatory for all rewrites?

后端 未结 2 1199
野的像风
野的像风 2021-01-18 05:34

I was just wondering if it would be necessary to include the RewriteEngine On and Rewrite Base / each time I add a rewrite? For example I would like to use both the \"Remove

相关标签:
2条回答
  • 2021-01-18 05:58

    You need RewriteEngine On in each of the htaccess files that have rewrite rules, but only once. Otherwise the rules will be ignored.

    As for the RewriteBase, you only need that if you have relative URI's as targets, or if you want your rules to be portable (thus you only need to change the base if you move rules from one directory to another). For example:

    Don't need RewriteBase for these rules:

    RewriteRule ^login.php /login.php? [L]
    
    RewriteRule ^.*$ - [F,L]
    

    Because /login.php isn't a relative URI, so it's understood that you're literally referring to http://domain.com/login.php and the - just means pass-through so the base is ignored anyways.

    An instance where you may want to use RewriteBase:

    RewriteRule ^dir/(.*)$ dir2/$1 [L,R=301]
    

    Without a RewriteBase / apache will probably guess incorrectly whether dir2/$1 is a file or URI path and redirect you to the wrong place. With RewriteBase / then it'll redirect correctly.

    0 讨论(0)
  • 2021-01-18 06:12

    Yes, RewriteEngine and RewriteBase have to be specified only once.

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