I am trying to rewrite form http to https and www to non-www urls.
Basically if someone goes to http://www.example.com they need to be redirected to https://example.com and all other instances of wrong urls (http://example.com, etc.).
I tried to do this by piecing suggestions together:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^www\.garrysun\.com [NC]
RewriteRule ^(.*)$ https://garrysun.com/$1 [L,R=301]
But when I check the URL in a redirect checker I get errors... http://garrysun.com works fine and goes to https://garrysun.com but http://www.garrysun.com gets this:
Checked link: http://www.garrysun.com
Type of redirect: 301 Moved Permanently
Redirected to: https://garrysun.com/https://www.garrysun.com
How can I correct my rewrite code?
P.S. - I am using OpenCart 1.5.4 and I also have the following rewrite code above the new stuff (don't know if it affects anything):
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
You can use this single rule for that requirement:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://garrysun.com%{REQUEST_URI} [NE,L,R=301]
# rest of your rules will appear below
Related
I use SEO URL on my opencart using .htaccess, now I want to redirect all non-www URL access to URL with www. So I add these lines in my .htaccess file:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Yes now the URL is always redirected to www if it is written wihout www before my domain name, but I found something that I think it should not appear in the URL.
When I go to www.mydomain.com/category I tried to test that URL by removing the www (so it became mydomain.com/category) and hit ENTER. The URL was redirected to www.mydomain.com/index.php?_route_=category. I did not know what made the URL become like that, I think the URL should redirect to www.mydomain.com/category. I have tried to move the position of redirection lines before and after this line:
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
Here is the full .htaccess content:
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
# Redirect non-www to www (I have tried to move this lines but still resulting the same)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
What is wrong with my redirection lines? Any help and suggestion would be appreciated. Thank you.
The issue is that you've put the rewrite for the domain too low down. Move it to just after the RewriteBase / and it will execute before the url rewrites do
RewriteBase /
# Redirect non-www to www (I have tried to move this lines but still resulting the same)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
I see that all these scripts for removing .html extension seem not to work via SSL. I solved the issue with new script but now i am creating a 2 step redirect chain which i do not like for SEO reasons - and with your help :) i hope to get it back to a redirect step / hop.
My script is
RewriteBase /
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.carpro.ro/$1 [R=301,L]
# Force WWW prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Remove .html extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.*)\.html$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
but it seems to create a cascade of redirects.
Is there any better version which
301 http to https for all pages of the site
redirect non WWW to WWW
remove .html extension and do not leave a trailing /
The script above works but with redirect cascades which i do not like
http://www.domain.com/something.html
does a 301 Redirect
https://www.domain.com/something.html
then again a 301 Redirect
https://www.domain.com/something
rather than
http://www.domain.com/something.html
a single redirect to
https://www.domain.com/something
Any ideas on optimising this?
Try this code:
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://www.carpro.ro%{REQUEST_URI} [NE,R=302,L]
# Force WWW prefix
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=302,L]
# Remove .html extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.+?)\.html$ /$1 [L,R=302,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+?)/?$ $1.html [NC,L]
You can try below Rewrite Rules to eliminate html extension
RewriteRule ^(.*).html$ /$1 [R=301,L]
And the following rewrite rule for trailing / removal
RewriteRule ^(.*)/$ /$1 [R=301,L]
Hope this helps
Basically, i want to have these
non-www to www
http to https
http://domain.com to http://www.domain.com
My current htaccess is this.
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?url=$1 [QSA,NC,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1
It works fine this way, but when you manually go to http://domain.com, and change it to https://domain.com, it added two sets of index.php.
Any ideas please?
What I can see to be an issue with your code is that you have $1 on the WWW and HTTPS redirects, which would add the request URI path at the end twice, since you already have %{REQUEST_URI}:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}:443%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?url=$1 [QSA,NC,L]
Also it would be easier if you first redirect HTTP to HTTPS (if needed) and then check whether or not the WWWW is missing to redirect (again if needed).
Could probably leave the HTTPS rule without [R=301,L] but if the URL is already right we want to avoid processing more rules.
Keep in mind that the ORDER of your rules is very important.
First I directed my /example folder to main domain. Now, I'd like to redirect some of the pages to HTTPS. Pls, anyone could advice me about the code:
# com upload yonlendirme
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/example/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ example/index.php [L]
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
#redirect www.mydomain.com to mydomain.com (or any other subdomain)
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
#force https for certain pages
RewriteCond %{HTTPS} !=on
RewriteRule ^(index\.php?route=account/register|index\.php?route=account/login|index\.php?route=account/account|index\.php?route=checkout/checkout)$ https://%{HTTP_HOST}%{REQUEST_URI}[L,R]
You can't match against the query string (everything after the ?) in a rewrite rule. You'll need to match against the %{QUERY_STRING} variable in a rewrite condition:
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} ^route=(account/register|account/login|account/account|checkout/checkout)
RewriteRule ^index.php$ https://%{HTTP_HOST}%{REQUEST_URI}[L,R]
You may also want to move that rule up to the top of your htaccess file so the routing to the example folder doesn't interfere with the redirects.
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!