Using Rewrite Rules

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

AutoLoad .htaccess

With OpenLiteSpeed, you can load .htaccess from directories and subdirectories automatically, with directives given at the server level, virtual host level, or context level.

Server Level

In the OLS WebAdmin Console, navigate to Server Configuration > General > Rewrite Control and set Auto Load from .htaccess to Yes.

Virtual Host Level

In the OLS WebAdmin Console, navigate to Virtual Hosts > Your VH Name> Rewrite and set the following:

  • Auto Load from .htaccess: Yes
  • Enable Rewrite: YesAlternatively, you can make the changes by editing your virtual host configuration file directly:
rewrite  {
  enable                  1
  autoLoadHtaccess        1
}

Restart OLS after changing the file, so that the new configuration will be loaded from the virtual host’s root directory.

Context/Directory Level

For modern versions of OpenLiteSpeed, it is not necessary to instruct the Auto Load from .htaccess setting to browse particular subdirectories. Assuming the setting has been enabled at the virtual host level, and the directory is being accessed via HTTP, subdirectories will be automatically browsed.

If you are running an older version of OLS, pre v1.5.4, you must explicitly instruct OpenLiteSpeed to look for .htaccess in subdirectories. For example, to look in a directory called /dir/, add a context to your virtual host configuration, like so:

context /dir/ {
allowBrowse             1
}

You will need to add a context for each subdirectory.

Unsupported Directives

Unsupported directives will be ignored by OpenLiteSpeed.

OLS currently supports mod_rewrite rules from Apache, so any rules you add to .htaccess must be converted to Apache’s mod_rewrite format before loading. See the next section for more information.

TIP: Remember that you must restart OLS after making any changes to your rewrite rules so that the changes will take effect.

Migrate Apache Rewrite Rules to OpenLiteSpeed

Because they follow the same format, Apache rewrite rules can be usually copied and pasted as-is into OpenLiteSpeed’s WebAdmin Console. Navigate to Configuration > Virtual Hosts > your virtual host > Rewrite and paste the rules into the Rewrite Rules field.

Apache mod_rewrite syntax is slightly different when a rewrite rule is in an .htaccess file as opposed to a virtual host configuration (an httpd.conf file). (You can learn more about that here. ) So, you will notice in the scenarios below that there are a few situations where you will need to make minor changes to the rules for OpenLiteSpeed.

From Document Root .htaccess to VirtualHost Context tab

In the OpenLiteSpeed WebAdmin Console, navigate to VirtualHost > Edit your vhost > Context. Create a new static context with the following settings:

  • URI: /
  • Location: File location of the public directory for your web page, for example $VH_ROOT/html/
  • Accessible: Yes
  • Enable Rewrite: Yes
  • Rewrite Rules: Paste the Apache rewrite rules here, as-is, for example:
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule . /index.php [L]
    

From Document Root .htaccess to VirtualHost Rewrite Tab

In this scenario, you will need to make a small adjustment to the Apache rewrite rule before pasting it into the WebAdmin Console.

In this example, we are looking to match any URL that matches /adminpages/.

When Apache rewrite rules are configured at the directory level (as in this scenario), the part of the URL that is mapped to the directory is removed from the URL. In this case, that’s the preceding /, and Apache only needs to match the last part of the URL, the adminPages part.

Here is the rule as written for Apache:

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

When defining rewrite rules for OpenLiteSpeed in VirtualHost > Edit your host > Rewrite, you need to account for the fact that OLS doesn’t remove the directory part of the URL before evaluating the pattern. So, we need to make sure that OLS can match on both adminpages/ and /adminpages/. We do that by adding /? before the file name.

/? in RegEx, this indicates that there may or may not be a / in that position.

The rule would be rewritten for OpenLiteSpeed like so:

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

From Apache VirtualHost to OpenLiteSpeed VirtualHost

Unlike Apache rewrite rules defined in the document root, Apache rewrite rules defined at the vhost level do not remove the directory part of the URL before evaluating the pattern.

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), just like in the OpenLiteSpeed vhost configuration.

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 these rules defined in an Apache vhost:

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

You can copy those rules as-is to the VirtualHost > Edit your host > Rewrite tab:

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

For a Subdirectory

The RewriteBase directive is not supported by OpenLiteSpeed, so to migrate Apache rules that use it, you’ll need to make the directory a part of the rewrite rule.

For example, with this Apache rewrite rule in the document root .htaccess:

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

We would rephrase it for an OpenLiteSpeed virtual host configuration like so:

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

RewriteBase can also be used to direct traffic outside of the document root. To replicate this functionality in OpenLiteSpeed, use an OLS Context, as demonstrated in the first scenario. You can stipulate the URL prefix in the Context’s URI and Location settings.

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://www.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]