Search code examples
apache.htaccessmod-rewritehttp-redirect

htaccess to redirect root to subfolder


We have a GoDaddy hosting. Within public_html we have a number of folders, each for a different site.

The main site (mysite.com) should normally be in the public_html but we moved them to public_html/mysite. We had an htaccess file to handle this change.

mysite.com is a slim (PHP) site. Within its public folder is another htaccess file that redirects to the public subfolder.

We tried to redirect mysite to a different web address and something got broken. Now if I go to mysite.com it shows Forbidden. Also, the FTP no longer works. When I try to connect with Filezilla I get

    Connection attempt failed with "EAI_NONAME - Neither nodename nor servname provided, or not known".

This is because I try to connect to ftp.mysite.com. If I change that to an IP it does work. But clearly something is off with mysite.com.

The current htaccess within public_html/mysite is as follows:

RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule ^(?!public)(.*)$ /public/$1 [L]

Any ideas on how can I get this to work again? I think the problem is that the domain is pointing to the root of the public_html folder instead of public_html/mysite but no idea how to fix this.

UPDATE

We have an htaccess file in the public_html folder

RewriteOptions inherit

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule ^(?!mysite)(.*)$ /mysite/$1 [L]

If I remove it, all subdomains work but mysite.com shows Forbidden. If I enable the file, then every single subdomain, including mysite.com shows Internal Server Error.


Solution

  • You may have this .htaccess in site root:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
    RewriteRule ^(?!mysite/)(.*)$ mysite/$1 [L,NC]
    

    You should remove RewriteOptions inherit directive as that is not serving any purpose here.

    Also note some minor fixes in your rewrite rule.