RewriteEngine Required for all rewriters?

I'm just wondering if RewriteEngine On and Rewrite Base / need to be turned on every time I add a rewrite? For example, I would like to use both "Removes QUERY_STRING from URL ^" and "block access to files during certain hours of the day" (examples cited at askapache.com)

Removes QUERY_STRING from URL

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

      

block access to files at 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 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.

+3


source to share


2 answers


Yes, RewriteEngine

and RewriteBase

must be listed only once.



+4


source


You need RewriteEngine On

in each of the htaccess files that rewrite the rules, but only once. Otherwise, the rules will be ignored.

As for RewriteBase

, you only need this if you have a relative URI as targets or if you want your rules to be portable (whereby you only need to change the base if you move rules from one directory to another), For example:

Not needed RewriteBase

for these rules:

RewriteRule ^login.php /login.php? [L]

RewriteRule ^.*$ - [F,L]

      



Because it is /login.php

not a relative URI, so it understood what you were literally referring to http://domain.com/login.php

, but -

simply means pass-through, so the base is ignored anyway.

An example where you can use RewriteBase

:

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

      

Without RewriteBase /

apache, it will probably incorrectly assume that dir2/$1

is a file path or URI and redirect you to the wrong place. With help RewriteBase /

it will be redirected correctly.

0


source







All Articles