1. Home
  2. Web Content
  3. Rewrite rules fail on subdirectories, subdomains, or addon domains

Rewrite rules fail on subdirectories, subdomains, or addon domains

Overview

Rewrite rules remap a URL to another location or resource accessible on a web site. These rules are located in .htaccess files. A common snippet looks similar to:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} ! -f
RewriteRule ^(.*)$ index.php [QSA, L]

When located anywhere else besides the document root of the primary domain name, rewrite rules will fail yielding an Internal Server Error.

Cause

Rewrite rules modify the URL relative to a document root defined in the web server configuration as DocumentRoot. Each site may have one DocumentRoot defined and this value is always /var/www/html. Additional domains, subdomains, and resources located under /var/www/html are subject to filesystem remaps outside the location of DocumentRoot. RewriteBase is necessary to anchor a rule set to the new filesystem location.

Solution

Inside the .htaccess, immediately following RewriteEngine On, add RewriteBase / or if the .htaccess resides under a subdirectory that also appears in the URL path, then use that directory in the URL path, e.g. http://example.com/mysubsite .htaccess rewrite rules would require RewriteBase /mysubsite as opposed to RewriteBase /.

A revised example of the earlier snippet would read as follows:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} ! -f
RewriteRule ^(.*)$ index.php [QSA, L]
Updated on March 7, 2021

Related Articles