I'm having issues setting up htaccess properly and was hoping to get some help getting it squared away. Currently I have my primary domain on my shared hosting routed to /public_html/example.com (for consistency's sake) using the following htaccess in the /public_html/ folder:
# .htaccess main domain to subdirectory redirect
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} !^/example.com/
# 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 ^(.*)$ /example.com/$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 ^(/)?$ example.com/ [L]
That works all fine but now I want to add lines to force WWW and to force HTTPS. I have a htaccess file that works great for my subdomains but when I add that to my /public_html/example.com/.htaccess file it creates an odd behavior. Any traffic to example.com redirects to https://www.example.com/example.com . This code is shown below.
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can anyone help me merge the two so I get the directory relocation to /public_html/example.com, forced WWW and forced HTTPS all in one go? Thank you very much in advance!
This is being caused by another rule in your .htaccess
. The rule in question is:
RewriteRule ^(/)?$ example.com/ [L]
Or to be more precise the [L]
flag at the end of the rule. Which stands for LAST
. This is telling your .htaccess
to stop any rules pass this rewrite rule.
You can simply fix this by either putting your new rules to the top of your .htaccess
, removing the [L]
flag or by removing the other rule altogether.
Buyers choice.
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.