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
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.
Yes, RewriteEngine
and RewriteBase
have to be specified only once.