skip to Main Content

I’m using a cPanel account to host multiple sites. To have a clean public_html, I use .htaccess to redirect the main domain into one of the subfolders.

So when user types in www.example.com, it will be rewritten into www.example.com/mainsite/ but still showing www.example.com in the URL.

But when using the codes below, every domain (including sub domains) will get redirected to that folder:

RewriteCond %{REQUEST_URI} !^/example
RewriteRule ^(.*)$ /mainsite/$1 [NC,L]

How can I target only www.example.com (and www.example.com/files) to be rewritten to the /mainsite?

*Other subdomains should be pointing to their own Document_Root as created.

2

Answers


  1. You need one more RewriteCond to check for main domain:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^(?:www.)?example.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/mainsite/ [NC]
    RewriteRule ^(.*)$ /mainsite/$1 [L]
    
    Login or Signup to reply.
  2. Do this, This line helps me solve the issue.

    
        # .htaccess main domain to subdirectory redirect
        # Do not change this line.
        RewriteEngine on
        # Change example.com to be your main domain.
        RewriteCond %{HTTP_HOST} ^(www.)?example.com$
        # Change 'subdirectory' to be the directory you will use for your main domain.
        RewriteCond %{REQUEST_URI} !^/subdirectory/
        # Don't change the following two lines.
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        # Change 'subdirectory' to be the directory you will use for your main domain.
        RewriteRule ^(.*)$ /subdirectory/$1
        # Change example.com to be your main domain again.
        # Change 'subdirectory' to be the directory you will use for your main domain
        # followed by / then the main file for your site, index.php, index.html, etc.
        RewriteCond %{HTTP_HOST} ^(www.)?example.com$
        RewriteRule ^(/)?$ subdirectory/index.html [L]
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search