Need Help with Rewrite Rules Please :-)

Hello fellow Cyberpanel Forums members,

i’m trying since hours to get my head around on why are the following rewrite rules are not working. I had that project before running on Apache for years without any problem but want to switch to OLS now. Here are the rules:

AddType application/x-httpd-lsphp .php .phtml .html .htm
AddHandler application/x-httpd-lsphp .php .phtml .html .htm

RewriteEngine on
Options +FollowSymlinks
Options +MultiViews
RewriteBase /

RewriteCond %{HTTP_USER_AGENT} ^$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[0-9]\.[0-9]$ [NC,OR]

RewriteCond %{HTTP_USER_AGENT} ^.(BaiduSpider|YandexBot|AhrefsBot|SiteBot|SEOkicks|SISTRIX|SemrushBot|MajesticSEO|SMTBot|spbot|MJ12bot|BLEXBot).$ [NC,OR]

RewriteCond %{HTTP_USER_AGENT} ^.(ia_archiver|BacklinkCrawler|Twiceler|download|GigaBot|TechrigyBot|SBIder|Jakarta|BacklinkCrawler|MegaIndex).$ [NC,OR]

RewriteCond %{HTTP_USER_AGENT} ^.(Wget|Python|Java|Jakarta|WebAlta|Cityreview|DoCoMo|veoh-|relevantnoise|HTMLParser|Moreoverbot).$ [NC]

RewriteRule ^.* - [F,L]

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

RewriteRule ^/([-0-9a-z]+)/([-a-z]+)-([0-9]+)\.jpg$ /img/$3.jpg
RewriteRule ^/([-0-9a-z]+)\.html$ page.php?page=$1 [L]

RewriteRule ^/feed$ /feed/ [R=301]
RewriteRule ^/feed/$ indexfeed.php [L]

RewriteRule ^/([-0-9a-z]+)/feed$ $1/feed/ [R=301]
RewriteRule ^/([-0-9a-z]+)/feed/$ feed.php?city=$1 [L]

RewriteRule ^/([-0-9a-z]+)/([-0-9a-z]+)/contact-([-0-9a-z-]+)$ $1/$2/contact$3.html [R=301]
RewriteRule ^/([-0-9a-z]+)/([-0-9a-z]+)/contact-([-0-9a-z-]+).html$ contact.php?city=$1&topic=$2&page=$3 [L]

RewriteRule ^/([-0-9a-z]+)/([-0-9a-z]+)/send-([-0-9a-z-]+)$ $1/$2/send$3.html [R=301]
RewriteRule ^/([-0-9a-z]+)/([-0-9a-z]+)/send-([-0-9a-z-]+).html$ send.php?city=$1&topic=$2&page=$3 [L]

RewriteRule ^/([-0-9a-z]+)/([-0-9a-z]+)$ $1/$2/ [R=301]
RewriteRule ^/([-0-9a-z]+)/([-0-9a-z]+)/$ topic.php?city=$1&topic=$2 [L]

RewriteRule ^/([-0-9a-z]+)$ $1/ [R=301]
RewriteRule ^/([-0-9a-z]+)/$ topic.php?city=$1 [L]

On restarting OLS at the OLS Panel i’m getting 2 errors wich are saying:

Invalid rewrite directive: AddType application/x-httpd-lsphp .php .phtml .html .htm
Invalid rewrite directive: AddHandler application/x-httpd-lsphp .php .phtml .html .htm

1.) Question: How do i tell OLS to process .htm and .html files as php?

2.) Question: Is there anything wrong with my rewrite rules above? I added the Slash after the caret already but with all the sprinkled and sometimes incomplete tips on how to convert Apache rewrite to Litespeed rewrite rules, i just wasn’t able to make these things run smooth.

I’m thankful for every help and hint. Thank you very much for your time.

Best regards, Thomas

Not supported by OLS

AddType application/x-httpd-lsphp .php .phtml .html .htm
AddHandler application/x-httpd-lsphp .php .phtml .html .htm

RewriteEngine on
Options +FollowSymlinks
Options +MultiViews

To handle html and htm files as php, open your vHost Conf and convert

scripthandler  {
  add                     lsapi:appName php
}

to

scripthandler  {
  add                     lsapi:appName php, html, html
}

You are a dream, thank you :slight_smile:

Options +FollowSymlinks
Options +MultiViews

are not supported. But “RewriteEngine On” is supported i assume?
By the way, i’m including all the above lines not in a .htaccess but inside the Rewrite Rules section in Cyberpanel. If that makes a difference?

So, the first question is answered. Please let me know if you see any mistakes at my rewrite rules.

Thank you, best regards, Thomas

Yes, RewriteEngine is supported. CyberPanel Rewrite Section just populates the .htaccess file for you and restart the OLS.

You don’t need forward slash anymore, kindly visit https://cyberpanel.net/2018/09/10/openlitespeed-v1-4-38-has-been-released/

Very useful information, thank you. Since i’m already on OLS 1.4.43 that is included already.
But i just noticed something. I have Gzip enabled at OLS / Cyberpanel and my php scripts have still ob_start(“ob_gzhandler”); in the head section.
Maybe the double zipping is the culprit here. I’m testing and will report back.

What would be the more ressource friendly way of compressing dynamic files? By php od better directly through OLS?

Thank you :slight_smile:

Server-side compression is enough I guess.

Just wanted to thank you again. Everything is working like expected. The new .htaccess compatibility is an absolute dream :slight_smile:
I did not need to tweak anything more than described above.

The errors i had where related to a few <? short open tags in php and after replacing them with proper <?php tags everything runs as fast as a flash :slight_smile:

I’m still having some problems with some rewrite rules and still need a bit of help since i can’t seem to find out what’s the reason. This rewrite rules are working fine on Apache.

with the below actual rewrite rules:

content of the childs domain rewrite rules at cyberpanel starts here

RewriteEngine On
RewriteBase /

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

RewriteRule ^([-0-9a-z]+)/([-a-z]+)-([0-9]+)\.jpg$ /img/$3.jpg

RewriteRule ^([-0-9a-z]+)\.html$ page.php?page=$1 [L]

RewriteRule ^feed$ /feed/ [R=301]
RewriteRule ^feed/$ indexfeed.php [L]

RewriteRule ^([-0-9a-z]+)/feed$ $1/feed/ [R=301]
RewriteRule ^([-0-9a-z]+)/feed/$ feed.php?city=$1 [L]

RewriteRule ^([-0-9a-z]+)/([-0-9a-z]+)/contact-([-0-9a-z-]+)$ $1/$2/contact$3.html [R=301]
RewriteRule ^([-0-9a-z]+)/([-0-9a-z]+)/contact-([-0-9a-z-]+).html$ contact.php?city=$1&topic=$2&page=$3 [L]

RewriteRule ^([-0-9a-z]+)/([-0-9a-z]+)/send-([-0-9a-z-]+)$ $1/$2/send$3.html [R=301]
RewriteRule ^([-0-9a-z]+)/([-0-9a-z]+)/send-([-0-9a-z-]+).html$ send.php?city=$1&topic=$2&page=$3 [L]

RewriteRule ^([-0-9a-z]+)/([-0-9a-z]+)$ $1/$2/ [R=301]
RewriteRule ^([-0-9a-z]+)/([-0-9a-z]+)/$ topic.php?city=$1&topic=$2 [L]

RewriteRule ^([-0-9a-z]+)$ $1/ [R=301]
RewriteRule ^([-0-9a-z]+)/$ topic.php?city=$1 [L]

content of the childs domain rewrite rules at cyberpanel ends here

when i’m calling a url like:
mydomain.com/one-city/ (with trailing slash) everything works fine
when i’m calling the url without trailing slash, like so:
mydomain.com/one-city
it redirects visible to:
mydomain.com/topic.php?city=one-city
This should not be. It should redirect with a 301 to
mydomain.com/one-city/
and call the script topic.php with the parameter city=one-city invisible for the visitor.

This misbehaviour happens only at the last 2 pairs of rewrite rules, i.e. the last 4 lines.

What am i doing wrong here?

Problem solved :slight_smile:

in order to solve the problem with the missing trailing slash i added right after the
RewriteRule (.*) http://mydomain.com/$1 [R=301]

the following 2 lines:

RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301]

Everything works now as expected.

If you have a similar problem and are testing around with rewrite rules, don’t forget to either clear your browser cache everytime you are testing a new rule, or use chromium/chrome with the Developer Tools and mark “disable cache”.