I use a LiteSpeed web hosting server on *.example.com and I am currently using this code in my .htaccess file for it:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/%1 [L,R=302]

This code makes 1.example.com redirect to example.com/1, which is great. But there are a couple of things that need fine-tuning with this:

  • 1.2.example.com redirects to example.com/1.2, but I would rather 1.2.example.com redirect to example.com/2/1 if possible.

  • Files are also inaccessible when this .htaccess code is present. I would like files to be accessible too.

(Yes, a multi-level wildcard is set up and working).

1 Answer 1

RewriteRule ^(.*)$ https://example.com/%1 [L,R=302]

Currently, you are capturing the URL-path (ie. ^(.*)$) but not making use of this, so any requests for files are redirected back to the "subdomain" subdirectory (which I assume is what you mean by being "inaccessible").

You are also missing the trailing slash on the target URL (ie. https://example.com/%1), so assuming this is a subdirectory then a second redirect will occur that auto-appends the trailing slash. (Appending the URL-path - see below - naturally resolves this issue.)

1.2.example.com redirects to example.com/1.2, but I would rather 1.2.example.com redirect to example.com/2/1 if possible.

Assuming you only have subdomains and sub-subdomains then I would implement this as a separate rule.

UPDATE: it somehow needs to first check to see if a file exists before performing the wildcard redirect.

Ah, OK, if you are intending to serve static files from the subdomain then you'll need to include a filesystem check to make sure the requested file does not already map to a file.

Try the following instead:

# Subdomain redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule (.*) https://example.com/%1/$1 [R=302,L]

# Sub-Subdomain redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.example\.com [NC]
RewriteRule (.*) https://example.com/%2/%1/$1 [R=302,L]

The $1 backreference contains the URL-path captured from the RewriteRule pattern. Whereas %1 (and %2) are backreferences to the preceding RewriteCond (the last matched CondPattern).

(.*) is the same as ^(.*)$ since regex is greedy by default.

  • That's working. Thanks. The trailing slash isn't needed though, so I modified it slightly. Jun 28 at 13:29
  • 1
    @RyderCragie What do you mean by "Files are also inaccessible ... I would like files to be accessible too."? What should happen when you request something like foo.example.com/image/file.jpg? Do you still need to be able to access files on the subdomain (or sub-subdomain)?
    – MrWhite
    Jun 28 at 13:30
  • 1
    @RyderCragie I'm also assuming that the subdomain should redirect to a subdirectory off the main domain (not a file or anything). Is that the case?
    – MrWhite
    Jun 28 at 13:34
  • 1
    @RyderCragie Ah, OK - you'll need to add a filesystem check. Sorry, I got the sub-subdomains round the wrong way (I didn't just notice you wanted these reversed). I've updated my answer. However, when redirecting to the subdirectory (off the main domain) do you want to discard the URL-path? eg. 1.example.com/does-not-exist.txt to example.com/1/ (note the trailing slash)?
    – MrWhite
    Jun 28 at 13:42
  • 1
    @RyderCragie "Currently it isn't." - Have you added the filesystem conditions in my updated answer? "I have removed the trailing slash" - So, you are not redirecting to a directory? Are you redirecting to a file? But without a file-extension this needs additional processing (is this related to your extensionless URLs from previous questsions)?
    – MrWhite
    Jun 28 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.