RewriteBase is a useful server directive available for Apache web server that allows you to easily update numerous rewrite rules at one go. In this article, we will look at what is RewriteBase and how it works, with examples.
What is RewriteBase
RewriteBase directive allows you to easily set the beginning of all relative paths used in .htaccess file. Its value is used in the target/destination path mentioned in rewrite rules in .htaccess file. Also it is applicable only for rewrite rules mentioned in the same .htaccess file where RewriteBase directive is mentioned. It is useful if you have numerous Rewrite rules in your .htaccess file. For example, if you want to move all these URLs to new directory, then just change the RewriteBase value without changing any Rewrite rule that follow.
Also read : How to Change Apache Log Level
How Does RewriteBase Work With Example
By default, all relative paths used in .htaccess file are with respect to the location of .htaccess file. For example, if your .htaccess file is located at /folder, then all relative paths in .htaccess file will begin with /folder. If you have the following RewriteRule,
RewriteRule /sub-dir1 /sub-dir2
then Apache will redirect all requests from /folder/sub-dir1 to /folder/sub-dir2
RewriteRule allows you to change this. Here is the syntax of RewriteBase directive
In the above statement you need to mention relative path for RewriteBase. This path is appended at the beginning of every destination relative path used in RewriteRule.
Let us look at an example. Let us say you have the following RewriteBase rule.
If you have the following RewriteRule after the above statement.
RewriteRule /product /about
This statement will redirect all requests from /product to /data/about
If the .htaccess file, that contains this rule, is located in /folder then Apache will redirect all requests from /folder/product to /data/about
Here’s a sample .htaccess configuration with RewriteBase & RewriteRule rule.
RewriteBase /data/RewriteRule /product /about
Please make sure you have enabled mod_rewrite in Apache server before using RewriteBase directive. Otherwise here are the steps to do it.
Also read : How to Prevent SQL Injection in Apache/PHP
Open terminal and run the following commands to enable mod_rewrite, depending on your Linux system.
Open terminal and run the following command to enable mod_rewrite
$ sudo a2enmod rewrite
Open Apache configuration file in a text editor.
$ sudo vi /etc/apache2/httpd.conf
Look for the following line.
#LoadModule rewrite_module modules/mod_rewrite.so
Uncomment it by removing # at its beginning. If you don’t find this line, add it afresh.
Also look for the following Directory tag and change AllowOverride from None to All.
. . .
. . .
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
. . .
. . .
Also read : Apache File Permissions Best Practices
In this article, we have learnt what is RewriteBase and how to use. It basically helps you set the beginning of all target relative paths for rewrite rules mentioned in your .htaccess file. It works only on destination paths of your rewrite rules.