I've used the 'Redirect mapper' tool by Varvy.com, and it's highlighted an issue with my www redirect. I'd like there to be only one redirect, but the tool shows two redirects. See screenshot below.
This is what I'm using in my htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
As the screenshot shows, http://www.d3creative.uk 302 redirects to https://www.d3creative.uk/, then 301 redirects to https://d3creative.uk/
I'd like to http://www.d3creative.uk/ to 301 redirect to https://d3creative.uk/, skipping the www redirect.
Any help would be much appreciated!
UPDATE
I use ServerPilot to manage my server, and I've setup SSL using ServerPilot.
I've removed the original RewriteCond and RewriteRule lines from my htaccess file
This clears up the multiple redirects (see screenshot below), but I still need a way to redirect www to non-www
I've contacted ServerPilot support, I'll post back here with any answers.
Fixed!
Within the ServerPilot control panel there is an option to force a HTTPS redirect. This was conflicting with any redirects I was using in my htaccess file.
Turn 'Redirect to HTTPS' off and use the ruleset below in your htaccess
ServerPilot support kindly provided this ruleset, which works great.
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
All versions of my domain now redirect (once) to my preferred version of the domain.
Related
in .htaccess Am using :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/$1/ [R=301,L]
It works fine for main domain i.e. redirects example.com to https://www.example.com
but not works on sub pages like
site.com/blog not redirects to https://www.example.com/blog
also like to add tailing slash '/' after url
https://www.example.com/blog/
how to achieve this?
Thanks in advance!
You should have htaccess rule like this. Make sure you keep these rules at the top of your htaccess file. Please clear your browser cache before testing your URLs.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI}/ [NE,R=301,L]
I know it has been asked many times here, but can't find the right configuration for my setup.
Requirements:
http://www.example.com & https://www.example.com (& example.com) have to redirect to https://test.example.com . (Notice it is a subdomain)
https://sub_1.example.com is to be EXCLUDED and not redirected. (Notice the subdomain has an 'underscore')
What I Have:
RewriteEngine On
RewriteBase /
# EXCLUDE following sub-domains.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^sub_1\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^test\.example\.com$ [NC]
# REDIRECT to
RewriteRule (.*) https://test.example.com/$1 [R=301,L]
>> ISSUE:
My https://sub_1.example.com is in fact an API that feeds a mobile app. When I use the above code, the mobile fails. So it seems that the sub-domain is not properly excluded and protected from the re-direct.
Would appreciate some help in cleaning it up.
FIXED !!
Needed to add an exception before the existing rules, since I needed to exclude https://sub_1.example.com/api
RewriteCond %{HTTP_HOST} ^sub_1\.example\.com [NC]
RewriteRule ^api - [L]
I've trawled many forums and tried many solutions. None work correctly. I am using ISAPI Rewrite 3 for IIS.
I need to change all requests to our website to WWW and HTTPS.
For example:
https://example.com/a-page-here/
http://example.com/a-page-here/
http://www.example.com/a-page-here/
www.example.com/a-page-here/
example.com/a-page-here/
to all change to:
https://www.example.com/a-page-here/
I've used http://htaccess.madewithlove.be, which may be buggy because I'm getting seemingly incorrect results for so-called working solutions. I don't want to be testing umpteen things on the live site.
This supposedly correct example (one of many) I found gives incorrect results:
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !443
# Extract non-www portion of HTTP_HOST
RewriteCond %{HTTP_HOST} ^(www\.)?(.*) [NC]
# Redirect to HTTPS with www
RewriteRule (.*) https://www.%2/$1 [R=301]
Example tests:
example.com/a-page-here/ = https://www./example.com/a-page-here
www.example.com/a-page-here/ = https://www./www.example.com/a-page-here/
Can anyone give me a set of rules that will cleanly and reliably turn any non www request to our website to the correct https://www version, and not add invalid slashes etc?
Try this one:
RewriteEngine On
# non-www to www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https\://www.example.com/$1 [R=301]
# HTTP to HTTPS
RewriteCond %{HTTPS} off [NC]
RewriteRule (.*) https\://www.example.com/$1 [R=301]
I (sometimes) somehow manage .htaccess file successfully but need help on this one.
I'd like to achieve the following at the same time
Redirect anything non-www to www
Redirect anything with /site or /site/ to root
Redirect anything http to https
In the end, every link, no matter how it originated, should begin with https://www.domain.com/ and end with /original_request_directory_or_file_if_any
I can get #1, no problem
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/" [R=301,L]
I can get #2 no problem and it works together with #1
RedirectMatch 301 ^/site/$ http://www.domain.com/
What I am unable to achieve so far is #3
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
When I enable the above code, I get browser error for redirect loop errors.
Looking forward to a solution (because I am sure it's possible and I just don't know how.)
Thank you all in advance!
If you redirect all to https, avoid to redirect before to http.
RewriteEngine on
RewriteRule ^site/? https://www.domain.com/ [R=302,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=302,L]
Change [R=302,L] to [R=301,L] when everything works well
I have a site that has been up for some time. I had a blog on a subdomain for some time. I have decided to do away with the main site and just support the blog subdomain.
I have a redirect setup for this, but it carries all the extra parameters through to the blog which results in a file not found page appearing. I just want the redirect to go to the index page without parameters.
What I currently have in my .htaccess file is this
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?(.*)$ "http\:\/\/blog\.foo\.org\/index\.php" [R=301,L]
When I get a request to
http://www.foo.org/foo/foo/?module=foo
it redirects to
http://blog.foo.org/foo/foo/index.php?module=foo
I want it to redirect to
http://blog.foo.org/index.php
You have to specify the query in the replacement to override the original:
RewriteCond %{HTTP_HOST} !^blog\.example\.org$ [NC]
RewriteRule ^ http://blog.example.org/index.php? [R=301,L]
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^(www\.)?foo\.org$ [NC]
RewriteRule ^ http://blog.foo.org/ [R=301,L]