best htaccess multi-rewrite rule - php

I need to find the best rewrite rule for my website...
I have 2 domains example.it and example.com but I have SSL under example.com only.
What I need: each user request should be redirected to https://www.example.com/...
Now I'm using this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.it$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
but if I digit http://example.com its redirect me to https://example.com, not www.
Also if I do a GTMetrix test I got an F value under Avoid landing page redirects:
Avoid landing page redirects for the following chain of redirected URLs.
http://example.it/
http://www.example.com/
https://www.example.com/
how can I solve? Thanks.

You can do all this in a single redirect rule and avoid multiple redirects. Have your .htaccess containing this rule only:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
Make sure to clear your browser cache when testing this.

Related

codeigniter - .htaccess redirection issue in http to https

When I try to redirect with or without www (example.com, www.example.com) To https://www.example.com, I am getting "The page isn’t redirecting properly" Error Message.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ "https\:\/\/example\.com\/$1" [R=301,L]
I tied below code also
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com%{REQUEST_URI}
But when i try to redirect without www To https://www.example.com is working fine.
I need to redirect with or without www example.com To https://www.example.com
Below code is redirecting (with or without www).example.com to https://www.example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?(example.com)$
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com/$1 [R=302,L]

I want to redirect specific url to non https using htaccess

my current htaccess is:-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
i want to add rule to htaccess for url like https://www.example.com/abc/arg1/arg2 should redirect to http://www.example.com/abc/arg1/arg2
for this https://www.example.com/abc/* it should redirect to non-https format with keeping all arguments.
Use below rule, check after clearing your cache.
RewriteCond %{REQUEST_URI} ^/abc/(.*) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Edit: Please try with below rule,
RewriteCond %{REQUEST_URI} !^/abc/(.*) [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
In above rule we are appending https to all url which are not starting with abc/(.*) you have to use this rewritecond where your original https rule exists.
If you want to redirect all https requests starting with abc/ to http, you must check for both abc/ and HTTPS
RewriteCond %{HTTPS} on
RewriteRule ^abc/ http://www.example.com%{REQUEST_URI} [R,L]
When everything works as it should, you may replace R with R=301 (permanent redirect). Never test with R=301.
I am not sure how you achieve the job, but the following should be working perfectly for you.
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/abc [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirecting HTTPS to Sub-Domain with .htaccess

I want to redirect both
https://website.me/ and https://www.website.me/ to https://es.website.me/
This rule doesn't work
RewriteCond %{HTTPS} !^on$
RewriteRule (.*) https://es.website.me/$1 [R,L]
Use below htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?website\.me$ [NC]
RewriteRule ^(.*)$ https://es.website.me/$1 [R=301,L]
Try this one for redirection in your case:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^website.me$ [OR]
RewriteCond %{HTTP_HOST} ^www.website.me$
RewriteRule (.*)$ https://es.website.me/$1 [R=301,L]
</IfModule>
Do you want to redirect, or rewrite?
To redirect with a code 301 (permanently moved), make 2 virtual hosts; one for the real site, and one for all the URL's you want to redirect. In the redirect host, add this line:
Redirect 301 / https://es.website.me/
Since you want to redirect only if HTTPS is already used, you must check for it, together with the hostname of course.
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(www\.)?website\.me$ [NC]
RewriteRule ^ https://es.website.me%{REQUEST_URI} [R,L]
When everything works as it should, you may replace R with R=301. Never test with R=301.

How to redirect from HTTPS to HTTP?

I have a Website. Where I need to redirect all the pages from HTTP to HTTPS. But there are two page which should not served over HTTPS.
Home Page - www.hellomysite.com
Dealers Page -www.hellomysite.com/dealers
Even if user has entered the url as https://www.hellomysite.com/dealers, it should be served over HTTP. http://www.hellomysite.com/dealers
I googled & found number of links but none of them is redirected.
.htaccess
#Redirect all request to HTTPS
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.hellomysite\.com*
RewriteRule ^(.*)$ https://hellomysite.com/$1 [L,R=301]
#RewriteCond %{HTTPS} on [OR]
#RewriteRule ^https://hellomysite.com/dealers$ http://hellomysite/dealers [R=301,L,QSA]
If I try anything more, then I get an error on opening the site as
This website has too many redirects
How do I redirect Home Page & the dealers page to HTTP.
If i understand you the following code will solve it :
RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteCond %{SCRIPT_FILENAME} !\/index\.php [NC]
#the above line will exclude https://www.hellomysite.com/index.php
# from the following rules
RewriteCond %{SCRIPT_FILENAME} !\/dealers\.php [NC]
# the above line will exclude the https://www.hellomysite.com/dealers.php
# from the following rules
RewriteRule (.+) https://%{HTTP_HOST}/$1 [L,R=301]
# above line will force every pages and directories ,except those who
# excluded above and the main root , to redirect from http to https
# (.+) means not to consider http://www.hellomysite.com/ and if you
# change it by (.*) it will be considered
Now you can force entire web site to redirect from http to https except www.hellomysite.com and www.hellomysite.com/dealers.
Note : please make sure that you empty browser cache before testing above code
Try :
#Redirect all request to HTTPS
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule !^$|dealer https://hellomysite.com%{REQUEST_URI} [L,R=301]

htaccess, redirect to https for a specific file and to non-www https

I've a site to work and trying to make redirect by htaccess for few days. I've searched the net and found good works, especially in this site, but altough I've tried almost every possibilities, I've could not achieve what I want to do.
My need is redirect all site to non-www http, including https, except for only one file. Let's say redirect
http ://www.example.com/.../....php?a=...
https ://www.example.com/.../....php?a=...
https ://example.com/.../....php?a=...
to
http ://example.com/.../....php?a=...
However, only one specific file
http ://www.example.com/.../theSpecificFile.php?a=...
https ://www.example.com/.../theSpecificFile.php?a=...
http ://example.com/.../theSpecificFile.php?a=...
should be redirected to
https ://example.com/.../theSpecificFile.php?a=...
To do these, I've wrote many htaccess files but in each case, I couldn't achieve my needs. e.g. At the last htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
ErrorDocument 404 http://example.com/404.php
#force https for certain pages
RewriteCond %{HTTPS} !=on
RewriteRule ^(theSpecificFile\.php)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
#redirect www.example.com to example.com (or any other subdomain)
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
By this htaccess file,
when I try to https ://www I get Unsecure or Untrusted connection (I'm translating from another language, hope it might be true translation) with ssl_error_bad_cert_domainand when I try to access to the theSpecificFile.php I get error defining "infine loop" (again I hope this might be a true translation).
This is really frustrating for me, so, any help would be highly appreciated.
Thanks in advance
Look, here you redirect theSpecificFile.php to its https version, but then the second redirect rule triggers and redirects the browser back to http, and from there the first rule redirects back to https... That's why you're getting the infinite loop.
And ssl_error_bad_cert_domain means the certificate isn't for this domain, so you might need to check if you're using the right one.
You can use that:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{REQUEST_URI} !/theSpecificFile\.php [NC]
RewriteRule ^ http://example.com/%{REQUEST_URI} [L,R=302]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /theSpecificFile\.php [NC]
RewriteRule ^ https://example.com/%{REQUEST_URI} [L,R=302]
Change [R=302,L] to [R=301,L] when everything works well
Please try:
RewriteCond %{HTTPS} on
RewriteConf %{REQUEST_URI} !=/theSpecificFile.php
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]

Categories