简体   繁体   中英

Redirecting naked domain to www. in a subfolder

I am hosting multiple domains on one hosting account, so I have made folders in the public_html folder for each domain.

eg home/username/public_html/examplewebsite.com/ and home/username/public_html/otherexamplewebsite.com/

I've been trying to figure out the code I need in my htaccess file to redirect visitors to the appropriate subfolder when they type in examplewebsite.com so that they don't see that they're in a subfolder of the main domain. I found this code originally, which was working:

Options -Indexes +SymLinksIfOwnerMatch 
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/domainfolder/
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.
#Rewrite all those to insert /folder
RewriteRule ^(.*)$ /domainfolder/$1 [L]

but I also want to redirect all visitors who type in the naked domain to the www. version. I found this code, but when I use them both in the htaccess file you can see the subfolder name in the url again:

#Only www, no naked domain
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

I don't really understand much of all this code so If any of you gurus could help me figure out a solution I would really appreciate it!

Keep your rules like this in root .htaccess:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTP_HOST} ^(www\.)?domainname\. [NC]
RewriteRule !^domainfolder/ domainfolder%{REQUEST_URI} [L,NC]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM