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.
Yes, RewriteEngine
and RewriteBase
have to be specified only once.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With