1

Litespeed obeys .htaccess rules but I cannot seem to get it to work for what should be a basic problem for which I cannot find a solution online. I want to redirect from this with everything in the sub-directory:

https://example.com/mail/

to

https://mail.example.com/

What I have tried so far that does NOT work:

SETUP 1

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^/mail$ 
RewriteRule ^(.*)$ https://mail.example.com/$1 [R=301,L] 

SETUP 2

RewriteRule ^mail/(.*)$ https://mail.example.com/$1 [R=301,NC,L]

SETUP 3

Redirect 301 /mail/ https://mail.example.com/

I have also tried another 5 things that don't work. The mail.example.com server is different from the one example.com/mail/ is sitting on.

EDIT TO ANSWER COMMENTS:

I have put the .htaccess file both in the root directory and the mail sub-directory but it does not redirect. By not working, I mean that the page just goes back to the original URL (example.com/mail/) instead of redirecting as expected.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Lethalmiko
  • 11
  • 4
  • Where are you putting this `.htaccess` file? Is `/mail` a physical subdirectory in the document root? Do you have any other directives in this `.htaccess` file? Any other `.htaccess` files along the filesystem path? – MrWhite Dec 15 '21 at 00:16
  • And "not work" means? Nothing happens? Undesirable redirect? Error? – MrWhite Dec 15 '21 at 00:27
  • I have edited original post to reflect answers to questions. – Lethalmiko Dec 15 '21 at 05:29
  • Is this a shared server environment? What do you see at `/mail`? Is this a special host-controlled subdirectory (that is perhaps an _alias_ to another server location, eg. used to access webmail)? Are you behind a caching proxy / CDN? Setup#2 should have worked in the root `.htaccess` file and Setup#3 should have worked in either the root or `/mail/.htaccess` file, providing you have no other conflicting directives. So there would seem to be something else going on here. Have you tried other redirects, outside of the `/mail` subdirectory? – MrWhite Dec 15 '21 at 10:20
  • @MrWhite It is a VPS where I have full root access. At /mail/, there is a webmail interface to an underlying email system. There are no aliases. There is Litespeed cache running on WordPress with a CDN but the mail sub-directory is totally separate and not part of the setup. The only other directives are from WordPress and Litespeed cache. Let me see about trying other redirects. – Lethalmiko Dec 18 '21 at 08:13
  • Please include your complete `.htaccess` file. So, the `/mail` subdirectory itself contains the webmail application? Are there any additional `.htaccess` file(s) in the `/mail` subdirectory file-path? – MrWhite Dec 18 '21 at 10:11

1 Answers1

1

Your setup #1 is almost OK. The reason why it failed is because the RewriteCond %{HTTP_HOST} ^/mail$ is not matched. You need to change the pattern ^/mail$ to (www\.)?example\.com$ where example.com is your main domain and place the code in /mail/.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$  [NC]
RewriteRule ^(.*)/?$ https://mail.example.com/$1 [R=301,L]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Amit Verma
  • 40,709
  • 21
  • 93
  • 115