Rewrite rule difference in .htaccess vs VH rewrite tab vs VH context rewrite rules

How Can We Help?
< Back
You are here:
Print

Rewrite rule difference in .htaccess vs rewrite tab vs context rewrite rules

For rewrite rules, OpenLiteSpeed uses the same syntax as Apache’s mod_rewrite so both servers can follow the same rules. However, why rewrite rules may be different in .htaccess than writing to rewrite tab in OLS? Actually it is the same behavior as Apache .htaccess rewrite rules vs Apache rewrite rules in virtual host configuration, which are different too.

The easiest way to use rewrite rules is to use rewrite rules in .htaccess without confusing.  If in any circumstance, you would like to use rewrite rules in OLS virtual host rewrite tab or OLS virtual host context level rewrite rules instead of .htaccess, please check the following to make sure you understand differences among OLS virtual host rewrite tab, OLS context rewrite rules and rewrite rules in .htaccess and how to convert rules from one location to another.

For example, if you migrate rewrite rules from the Apache document root .htaccess, or  /subfolder/.htaccesss, you will need to create a / context (to match the Apache document root) , or /subfolder/ context (to match Apache subfolders or directories). Then, place the exact rewrite rules there without any change.

If you migrate rewrite rules from Apache virtual host configuration, the exact same rules can be copied to OLS virtual host rewrite tab without any change.

If you copy rewrite rule from Apache doc root .htaccess to OLS virtual host rewrite tab directly, you may need to make some changes to make the rules work. Why? In Apache rewrite rules, sometimes virtual host level rewrite rules and .htacess level rewrite rules can be different. Within Apache, you may need to make changes when migrating rules from virtual host conf to .htaccess.  Similarly, you may need to make some changes when copying the rewrite rule from Apache doc root .htaccess (htaccess level rewrite) to OLS virtual host rewrite tab( virtual host level rewrite).

The following will explain these migration scenarios in detail.

1. Migrate Apache Rewrite Rules from Document Root .htaccess

Go to OpenLiteSpeed Webadmin > VirtualHost > Edit your vhost > Context tab. After that, create a new static context. The fields that need to be edited are:

URI – put here only /
Location – location of the public directory for your webpage
AccessibleYes
Enable RewriteYes
Rewrite Rules – Put the rewrite rules here

Here is an example image:

2. Migrate from Apache Document Root .htaccess to OpenLiteSpeed Vhost Rewrite Tab

Lets say that your document root is /home/user/public_html, and you have an .htaccess file there. In this file you have for example:

RewriteRule ^adminPages/(.*)$ admin-panel/$1 [L]

To Migrate that rules and that style of rules you will need to make small change. The new rule will look like this:

RewriteRule ^/?adminPages/(.*)$ admin-panel/$1 [L]

Put that rule in OpenLiteSpeed > VirtualHost > Edit your host > Rewrite tab.

As you can see we add /? before adminPage. That way OLS will know what to catch and redirect.

3. Migrate from Apache vhost to OpenLiteSpeed vhost

In a virtual host configuration, the rewrite rule pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. /app1/index.html). If you copy the rewrite rules for your webpage from the Apache vhost file you do not need to make any changes, as they will work in OpenLiteSpeed.

For example if you have in Apache vhost:

<VirtualHost *>
ServerName www.example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^/(.*)$ http://example.com/$1 [L,R=301]
</VirtualHost>

In OpenLiteSpeed, it will look the same:

RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^/(.*)$ http://example.com/$1 [L,R=301]

Put that rule in OpenLiteSpeed > VirtualHost > Edit your host > Rewrite tab.

Rewrite for Subdirectory

The RewriteBase directive is not supported by OpenLiteSpeed and you will need to make minor changes to your rewrite rules. For better understanding we will give you an example:

In an .htaccess file:

RewriteBase /joomla
RewriteRule .* index.php [F]

In a virtual host configuration (and in OpenLiteSpeed):

RewriteRule /.* /joomla/index.php [F]

Useful Examples

Default WordPress rules:

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^/index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

Redirect to HTTPS:

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.your-domain.com/$1 [R,L]

Redirect www to HTTPS:

RewriteCond %{HTTP_HOST} ^www\.your-domain\.com
RewriteRule (.*) https://your-domain.com/$1 [R=301,L]

Drupal 8:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^/ index.php [L]

Laravel:

RewriteRule . /laravel/public/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

CodeIgniter:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ index.php?/$1 [L]