Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RewriteEngine On mandatory for all rewrites?

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 "Removes the QUERY_STRING from the URL ^" and "block access to files during certain hours of the day ^" (Examples quoted from askapache.com)

Removes the QUERY_STRING from the URL

RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} .
RewriteRule ^login.php /login.php? [L]

block access to files during certain hours of the day ^

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# If the hour is 16 (4 PM) Then deny all access
RewriteCond %{TIME_HOUR} ^16$
RewriteRule ^.*$ - [F,L]

Can I possibly combine them into the following?

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} .
RewriteRule ^login.php /login.php? [L]
# If the hour is 16 (4 PM) Then deny all access
RewriteCond %{TIME_HOUR} ^16$
RewriteRule ^.*$ - [F,L]

Thanks in advance for your time.

like image 691
bernie Avatar asked Feb 18 '13 23:02

bernie


2 Answers

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

like image 184
nwellnhof Avatar answered Oct 13 '22 18:10

nwellnhof


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.

like image 32
Jon Lin Avatar answered Oct 13 '22 19:10

Jon Lin