Redirecting domain and subdomain from http to https - mod-rewrite

I want to redirect all http requests for my domain and subdomains to https. For example http://example.com should go to https://example.com and http://subdomain1.example.com should go to https://subdomain1.example.com, etc.
I have written this code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^(.*)$ https://%1.example.com$1 [R=302,L]
This code is only redirecting the subdomains but it is not redirecting the domain. What changes should I do the above code that it start routing for both.

You can use the following to redirect your domains to https :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

Related

.htaccess to rewrite one folder to http - all others to https

Currently I have an http rewrite rule to change all http requests to https. I want to do the reverse on one particular folder (change all requests to http for that folder), but I also want the child folders of that http folder to be rewritten to https.
This is my current .httaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/HTTP_FOLDER/* #want http on parent
RewriteCond %{REQUEST_URI} !^/HTTP_FOLDER/HTTPS_SUBFOLDER/** #want https on sub folder
RewriteRule ^(.*)$ "http\:\/\/mysite\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR] #all others rewrite to https
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
RewriteRule ^(.*)$ "https\:\/\/mysite\.com\/$1" [R=301,L]
I think it is actually working as posted. Something is cached on my machine. I tried it from several other computers and it appears to work.

Laravel redirects to 404 error page if URL has www prefix

I'm having project in Laravel 6.12. I've hosted my app in live server and having SSL configured.
It works well when I access with https://example.com, but when I try to access with https://www.example.com it shows laravel's default 404 error page.
It means, it is accessing the project directory, loading css, js etc but showing 404 error page.
my .htaccess has following lines
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R,QSA,L]
Could anyone help to get rid of this?
replace your .htaccess with this
if try https://example.com it will force redirect to https://www.example.com
This is true to SEO
RewriteCond %{HTTPS} =on
# check if not have www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# force redirect to www
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Can Director::forceWWW() and Director::forceSSL() be used together in SilverStripe?

I have a SilverStripe website which I'd like to always use the www domain prefix and always use https.
So:
http://example.com
http://www.example.com
https://example.com
Would all redirect to:
https://www.example.com
However when I put the following lines in /app/_config.php:
Director::forceWWW();
Director::forceSSL();
I get a redirect loop. The same thing happens if I swap the order:
Director::forceSSL();
Director::forceWWW();
Does this mean that Director::forceWWW() and Director::forceSSL() can't be used together?
What configuration should I use to get my desired outcome?
I think you are correct in that forceWWW() and forceSSL() can't be used together.
If our site is on an Apache server we can add redirect rules to our root .htaccess file to do these two redirects:
# ...
<IfModule mod_rewrite.c>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine On
# Redirect non www to www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# Redirect non https to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# ...

Rewrite subdomain to main domain for images only using htaccess

I have a domain, where everything except image\css etc. are handled by a single php file. However after a reorganisation, alot of images have been moved from sub-domains to the main domain. So I'm looking for a way to redirect all image\css files to the main domain if they were originally on one of the sub-domains. My current code is
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.ico|\.bmp|\.css|\.ts|\.js)$
RewriteRule !^index\.php$ /index.php [L]
I've tried a couple of ways to redirect it, but I seem to break on of the existing rules, whatever I try.
Thanks
The final solution I came up with
RewriteEngine On
# Check the request isn't for the main domain
RewriteCond %{HTTP_HOST} !^domain\.com$
# Check the request is for a static resource
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
# Redirect to main domain
RewriteRule (.*) http://domain\.com/$1 [R=301,L]
# if the request isn't for index.php,
# (invisibly) redirect to index.php
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.ico|\.bmp|\.css|\.ts|\.js)$
RewriteRule !^index\.php$ /index.php [L]
For information only. As really the credit goes to jon for the answer I just tweaked it for my needs.
If the URLs in your HTML still point to the subdomains, you'll need to setup htaccess redirects on those subdomains, not on the main domain, as the requests will still be going to the subdomains.
For example in /var/www/vhosts/sub.domain.com/httpdocs/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
RewriteRule (.*) http://domain.com/$1 [R=301,L]
And in /var/www/vhosts/sub2.domain.com/httpdocs/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
RewriteRule (.*) http://domain.com/$1 [R=301,L]
UPDATE
Based on your comment that Apache handles each subdomain as if it were the main one, try this:
RewriteEngine On
# Check the request isn't for the main domain
RewriteCond %{HTTP_HOST} !(www\.)?domain\.com$
# Check the request is for a static resource
RewriteCond %{REQUEST_URI} \.(png|jpg|gif|jpeg|ico|bmp|css|ts|js)$
# Redirect to main domain
RewriteRule (.*) http://domain.com/$1 [R=301,L]

magento https/http redirection

Is there a way to redirect domains based on its parameters in magento.
Eg:
if user trying to load https://stage.domain.com/ the system should redirect it to
http://stage.domain.com/,
if user trying to load https://stage.domain.com/all_access_profile the system should redirect it to http://stage.domain.com/all_access_profile ,
But the rest can goto its actual link what user actually requested.
I have got this one from #stackoverflow
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This one is always redirecting. How this needs to be changed to get url parameters based redirection.
Thanks
You want to redirect from the secure (https) to the insecure (http) but the rewrite rule http%1 is directing to the secure. That is the wrong way round so it should be:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS} ^on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Resources