1

I'm using litespeed on shared hosting (unfortunately), and I'm trying to use .htaccess to rewrite http to https NON www - and change domain.com/index to domain.com/my-string for SEO purposes.

This is working fine in Apache, and I didn't know my shared host was using Litespeed. I use the same .htaccess on litespeed which results in a 404, despite my filename being correct.

Here is my htaccess:

DirectoryIndex my-string.php

Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /

RewriteRule ^$ my-string [L]

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

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteRule ^my-string$ /my-string.php [L]

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.
<IfModule mime_module>
  AddType application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

The bottom section was added by my host.

This is working correctly in Apache, performs the 301 redirect from http non www, or http www to https non www with my string at the end of the URL, instead of index.

My understanding was litespeed uses the same syntax as Apache, so why is this happening?

user431951
  • 11
  • 2
  • So, your mod_rewrite directives aren't doing anything? Incidentally, if you've set the DirectoryIndex the way you have then you don't need the first internal rewrite (but that rewrite should come after the redirects anyway.) – MrWhite Aug 20 '17 at 17:22
  • @MrWhite Yes - I'm assuming that's whats happening. I was unsure whether litespeed needed anything different. I've tried looking at documentation online, but haven't found anything to solve this. Thanks for the pointers, this .htaccess really confuses me – user431951 Aug 20 '17 at 17:26
  • Yes, Litespeed should just work the same as far as I know. What is the URL you are requesting? – MrWhite Aug 20 '17 at 17:30
  • @MrWhite I've tried to request www.domain.com, domain.com which return a 404, BUT if I request www. or domain.com/my-keyword, then it works – user431951 Aug 20 '17 at 19:31
  • So, when you request `www.example.com/my-keyword` you are redirected to `https://example.com/my-keyword`? How is `/my-keyword` routed? Does this map to a filename? (You should certainly remove the first rewrite. ie. `RewriteRule ^$ my-string [L]`.) – MrWhite Aug 20 '17 at 19:46
  • If I request www. or no www. I get the bare domain (https) without "/my-keyword", the page loads properly but doesn't have the keyword at the end. The filename is my-keyword.php, so the file does exist. I just removed `/my-keyword` which improved things (didn't load www. before). – user431951 Aug 20 '17 at 20:27
  • I just removed `RewriteRule ^$ my-string [L]` I meant – user431951 Aug 20 '17 at 20:36
  • "The filename is my-keyword.php" - URLs containing `/my-keyword` (with the `.php` extension) are not handled by the code you have posted? (Which only refers to `my-string`.) Is that really what you are requesting on your Apache server? – MrWhite Aug 20 '17 at 20:43
  • @MrWhite I'm sorry - my filename is the same as the URL I'm requesting, so in my .htaccess I'm changing my `DirectoryIndex` to `my-string.php` and my file is called `my-string.php`. – user431951 Aug 20 '17 at 20:49
  • So the only thing that isn't working correctly is /my-string (with no PHP) at the end of the URL, and I've got the rewrites so I'm not sure why it's not working, as it's working fine on two of my Apache installs – user431951 Aug 20 '17 at 20:56
  • Try `Options -MultiViews` at the top of your `.htaccess` file on both your Apache and Litespeed servers. Any change? "the page loads properly but doesn't have the keyword at the end." - Are you saying the the _keyword_ (ie. `my-string`) is being stripped from the request?! – MrWhite Aug 20 '17 at 21:02
  • Adding `Options -MultiViews` didn't change anything. Yes it appears as though it's being stripped from my request, the page loads with HTTPS and no www., so my .htaccess must be interpreted correctly - I hadn't thought of it being stripped before you mentioned it to be honest – user431951 Aug 20 '17 at 21:43
  • If it's being "stripped" then _something else_ is doing that. Check the network traffic from the browser, what do you see? (But make sure your browser cache is cleared and you're not seeing a cached response.) – MrWhite Aug 20 '17 at 21:53
  • 1
    @MrWhite I've got in contact with my Shared Hosting company - this is one of the reasons why I hate using shared hosting, but unfortunately it's unavoidable for this site. I assume as my .htaccess rules are being ignored, it must be something to do with my host. Thanks for being so helpful, I really appreciate it. – user431951 Aug 20 '17 at 23:38
  • Do you happen to remember how you solved this by any chance? I'm having the same kind of issue... Shared hosting changed from Apache to LiteSpeed and my website started showing 404's and other kind of rewrite issues – Gabriel Bourgault Oct 01 '18 at 01:27

1 Answers1

0

The comments section is full so I'm linking my related answer:

https://serverfault.com/a/1083495/144798

To get local .htaccess working on Openlitespeed you first must generate those files AND then also point to those files within each virtual host AND ensure correct permissions. If you fail to do this, your websites might crash (fatal errors and/or 404 errors like this). Plus, you must reboot the Litespeed server every time you change .htaccess rules.

Jesse Nickles
  • 250
  • 1
  • 12