Park Lai
Park Lai

Reputation: 333

htaccess rewrite only main domain to subfolder

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.

Upvotes: 3

Views: 1995

Answers (2)

Ashif Sadique
Ashif Sadique

Reputation: 107

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]

Upvotes: -1

anubhava
anubhava

Reputation: 785551

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]

Upvotes: 3

Related Questions