I have domain and a subdomain like example.com and blog.example.com and enabled ssl for both using wildcard on bluehost. I add following code suggested by bluehost
Custom subdomain .htaccess SSL + WordPress
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.maindomain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^subdomain.maindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
# End custom subdomain .htaccess
# Custom maindomain .htaccess WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?maindomain.com$
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTP_HOST} ^(www.)?maindomain.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# End custom maindomain .htaccess
sites are running on https properly but they are accessible on http as well.
i tried following code to force redirect but its not working.
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
also i have two htaccess file for main domain and for subdomain and above code is placed in main domain htaccess file.
am i doing something wrong or missing something to force all urls to open with https:// ?
Have you tried this :
For only domain
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
For subdomain(place .htaccess inside subdomain folder)
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://blog.example.com/$1 [R,L]
Related
I have a main domain and many subdomains on it now i want to redirect main domain to its HTTPS and other domains to its subdomain for example:
main.com redirects to https://main.com
domain1.com redirects to domain1.main.com
domain2.com redirects to domain2.main.com
now i using this
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^www.main.com [OR]
RewriteCond %{HTTP_HOST} ^main.com [NC]
RewriteRule ^(.*)$ https://www.main.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
this code redirect main.com only and other domains must check in index.php and redirect to specific subdomain, i want to perform whole work in htaccess.
This probably is what you are looking for:
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(?:www\.)?main\.com
RewriteRule ^ https://www.main.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^(?:www\.)?main\.com
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.com
RewriteRule ^ https://%1.main.com%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
It makes sense to start out using a R=302 temporary redirection at first and only change that to a R=301 permanent redirection once everything works as desired.
Obviously this requires that requests to those other domains are handled by the http host which implements this rule set. So you most likely want to define either host aliases for those domains or use a default virtual host inside your http server.
Situation: afwezigheden.eu is the main domain with a few coupled domains (= aliases?) like skatespots.be, inviteme.eu, halftwaalf.be... Now I'm trying to redirect those aliasses to a subfolder, without the users seeing it.
Al those sites are in a subfolder _DOMAINNAME. For example: skatespots.be is in afwezigheden.eu/_skatespots
Also I have an SSL certificate, so I would like to redirect all domains to https too.
I tried several things, the last one is this one, where skatespots seems to work and everything is redirected over https.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/_skatespots/
RewriteCond %{HTTP_HOST} ^(www\.)?skatespots\.
RewriteRule ^(.*)$ /_skatespots/$1 [L]
RewriteCond %{REQUEST_URI} !^/_halftwaalf/
RewriteCond %{HTTP_HOST} ^(www\.)?halftwaalf\.
RewriteRule ^(.*)$ /_halftwaalf/$1 [L]
RewriteCond %{REQUEST_URI} !^/_inviteme/
RewriteCond %{HTTP_HOST} ^(www\.)?inviteme\.
RewriteRule ^(.*)$ /_inviteme/$1 [L]
RewriteCond %{HTTP:X-Forwarded-Proto} !http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
What am i doing wrong?
What if you put the folder structure as this:
afwezigheden.eu/sites/skatespots.eu
afwezigheden.eu/sites/halftwaalf.eu
And then use this in your afwezigheden.eu/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} !sites/
RewriteRule (.*) /sites/%{HTTP_HOST}/$1 [L]
I want to redirect all subdomains except www to https://www.example.com in .htaccess, how can I do this? All subdomains can be accessed.
I only want to redirect the subdomains, not inner pages on the main domain.
Examples: (Should redirect)
hello.domain.com
test.example.domain.com
www.hello.sub.domain.com
Examples of where not to redirect:
www.domain.com/hello-world
www.domain.com
Current .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.dk$
RewriteRule ^(.*) https://www.domain.dk/$1 [QSA,L,R=301]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This should get you started. Anything apart from www.mydomain.com will be redirected to www.example.com.
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*) https://www.example.com/$1 [QSA,L,R=301]
Edit:
Remember that it's the .htaccess in your subdomain folder, or _wildcard_domain.com that you will have to create or edit for this to work.
Try this in your .htaccess:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I have two domains, autodromodifranciacorta.it and franciacortacircuit.com both pointing to the same website hosted on this IP address: 94.23.64.40.
Now i want all the content to be under one single domain, so i decided to 301 redirect all the traffic from franciacortacircuit.com to autodromodifranciacorta.it
Here is my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^franciacortacircuit\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.autodromodifranciacorta\.it$ [NC]
RewriteRule ^(.*)$ http://autodromodifranciacorta.it/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The redirect is not working and i have no clue why, because the syntax looks correct to me.
The .htaccess file is processed, because if i put a typo in it, i get server error.
Wha'ts wrong with it?
Your second http_host condition is wrong and it never matches if the current host is franciacortacircuit.com, You need to use an OR condition to match against 2 diffrent hosts.
#Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^franciacortacircuit\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.autodromodifranciacorta\.it$ [NC]
RewriteRule ^(.*)$ http://autodromodifranciacorta.it/$1 [R=301,L]
I've looked "everywhere" but just can't seem to figure this one out.
I need the htaccess to do 2 things for me:
Force all www requests to non-www requests
Also force http (non-ssl) on all pages but one in which case I need ssl
Also, this is a wordpress site using permalinks, so I've got that chunk of htaccess code wordpress also puts in there. But somehow everything works fine, except for when I go to the page that needs to be forced to ssl, then it just redirects to the root of the site.
This is what my code looks like:
RewriteEngine On
RewriteBase /
# Redirect from www to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301]
# Turn SSL on for secure-page
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^secure-page[/]?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Turn SSL off everything but secure-page
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^secure-page[/]?$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
So results are:
any www and non-www and http urls are redirected to non-www and http -> OK
any www and non-www and https urls (excl. secure-page) are redirected to non-www and http -> OK
secure-page url are redirected to the site root and I can't figure out why -> NOT OK
Can someone please help?
Thanks
RewriteEngine On
# Redirect from www to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/secure-page[/]?$
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/secure-page[/]?$
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
# Turn SSL on for secure-page
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/secure-page[/]?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Turn SSL off everything but secure-page
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/secure-page[/]?$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
I've updated my answer!