I currently have two rewrite rules in my .htaccess file, and now need to add another. However, GTMetrix is already giving me an F rating here stating: Avoid landing page redirects
The first redirect adds the www. to the URL.
The second redirect adds a subdirectory to the URL.
The third (proposed) redirect adds the https to the beginning of the URL.
RewriteEngine on
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Then, rewrite to /catalog
RewriteCond %{REQUEST_URI} !^/catalog [NC]
RewriteRule ^(.*)$ /catalog/$1 [L]
#Now, rewrite to HTTPS:
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Is there any way to combine these into a single redirect? Or, is the landing page redirect not all that bad?
Your 1st and 3rd redirect rules can be combined into one and should be kept before rewrite rule:
RewriteEngine On
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
#Then, rewrite to /catalog
RewriteCond %{REQUEST_URI} !^/catalog/ [NC]
RewriteRule ^(.*)$ /catalog/$1 [L]
Make sure to clear your browser cache before testing this change.
Related
I want to achieve that if a user write:
http://www.example.com/url/index.html
http://www.example.com/url/
https://example.com/url/index.html
https://example.com/url/
will be always redirect to:
https://www.example.com/url/
So I write this redirect istruction in .htaccess (I'm not an expert of this so I think it could be here the problem) and it seems to work:
RewriteEngine On
RewriteCond %{ENV:HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
The problem is that from some SEO tools it seems that it make the work in more than 1 redirection. Infact I think that if the user look for:
http://example.com/url/
it redirects to
https://example.com/url/
after to
https://www.example.com/url/
How can I change the previuos code to make it better and make everything with 1 redirection?
Thanks
You can shorten your http to https redirect rules. Instead of using two separate rules for http and htttps://www you can just use a single rewrite rule that will handle both in one redirection .
RewriteEngine on
# http to https and non-www to www
RewriteCond %{ENV:HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^.*$ https://www.%1%{REQUEST_URI} [NE,R=301,L]
# remove index.html
RewriteRule ^(.*)\.index\.html$ /$1 [L,R=301]
Clear your browser cache before testing this htaccess.
I want to force redirect OpenCart store to its WWW version with opencart.
SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/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]
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This is the htaccess part for the redirect and it works but the smart URL now are
https://www.example.com/index.php?route=the-name-of-the-product
and it should be
https://www.example.com/the-name-of-the-product
Any idea how to fix this problem with the smart urls?
Am I doing the redirect wrong or the problem is other?
Try moving the following lines at the start of .htaccess file
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have a multistore setup on magento with multiple domains.
But I want just one specific store/domain to have https, and redirect all non https urls for that domain to https. Including the whole path.
For example all urls in this list to https://www.
Source urls:
http:// webwinkel.nl/willekeurige-categorienaam
www. webwinkel.nl/willekeurige-categorienaam
http:// www.webwinkel.nl/willekeurige-categorienaam
https:// webwinkel.nl/willekeurige-categorienaam
Target url:
https:// www.webwinkel.nl/willekeurige-categorienaam
I use this for a single store, and in that case it works perfect.
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But for a multistore this doesn't work, because it will redirect every store domain to https, but I want https only for one specific store.
Edit
#itoctopus: Thanx for your reply!
This works for www.webwinkel.nl.
But not for the other domains on the same multistore.
For example I have www.webwinkel.nl, www.webwinkel2.nl and www.webwinkel3.nl.
With your code, they all will redirect to www.webwinkel.nl.
This is my whole htaccess now:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)webwinkel.nl [NC]
RewriteRule . - [E=MAGE_RUN_TYPE:website]
RewriteCond %{HTTP_HOST} ^(.*)webwinkel.nl [NC]
RewriteRule . - [E=MAGE_RUN_CODE:webwinkel]
RewriteCond %{HTTP_HOST} ^(.*)webwinkel2.nl [NC]
RewriteRule . - [E=MAGE_RUN_TYPE:website]
RewriteCond %{HTTP_HOST} ^(.*)webwinkel2.nl [NC]
RewriteRule . - [E=MAGE_RUN_CODE:webwinkel2]
RewriteCond %{HTTP_HOST} ^(.*)webwinkel3.nl [NC]
RewriteRule . - [E=MAGE_RUN_TYPE:website]
RewriteCond %{HTTP_HOST} ^(.*)webwinkel3.nl [NC]
RewriteRule . - [E=MAGE_RUN_CODE:webwinkel3]
# First condition - redirect non-www to www for all domains
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Second condition - redirect HTTP to HTTPS for a particular domain
RewriteCond %{HTTP_HOST} ^webwinkel\.nl$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.webwinkel.nl/$1 [R=301,L]
Add the following code to your .htaccess file to ensure that only a particular domain gets redirected to https://www. The first condition is to handle redirection from non-www to www for all other domains. The second condition is for redirecting your domain to https.
RewriteEngine On
# First condition - redirect non-www to www for all domains
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Second condition - redirect HTTP to HTTPS for a particular domain
RewriteCond %{HTTP_HOST} ^webwinkel\.nl$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.webwinkel.nl/$1 [R=301,L]
for a website I need a redirect from
domain without www TO domain with www
and
http TO https
The final URL has to be always https://www.myshop.com ...
I solved this successfully with this commands:
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
The problem ist that I have to exclude a specific folder.
The URL to exclude is: http://www.mydomain.de/myfolder/filename.php
This means: If this URL is requestet there mustn't be a redirect.
Other tutorials I found did not work, sorry :-(
Thanks for any help and best regards...
You can have a skip redirect rule before these 2 redirect rules you have:
RewriteEngine On
# skip myfolder/filename.php from any redirects below
RewriteCond %{THE_REQUEST} /myfolder/filename\.php[?/\s] [NC]
RewriteRule ^ - [L]
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Maybe something like that
RewriteEngine on
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteRule ^http://www.mydomain.de/myfolder/filename.php$ - [L]
RewriteRule ^.*\filename.php$ http://www.mydomain.de/myfolder/filename.php [R=301,L]
Adding something like this as a first ruleset may help. (May want to revisit those [L,R] options though.
# check if myfolder is contained
RewriteCond %{REQUEST_URI} ^[^/]*/myfolder(/.*)?$
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
I currently use the following redirect to make the urls look nicer:
RewriteRule profile/(.*) index.php?id=$1
(Result: domain.co.uk/profile/00000001)
From index.php?id=00000001
I would like to 301 redirect urls without www to www. and keep the above profile redirect.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
However, When this is used the URL comes out like:
http://www.DOMAIN.co.uk/ index.php /00000001 ?id=00000001
I’m assuming this can be done, Does this need to be bespoke for my needs or is there something im missing?
End result would ideally rewrite:
domain.co.uk/profile/00000001
to
www.domain.co.uk/profile/00000001
Keep rules in correct order i.e. keep external redirects before your internal rewrites.
So this should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^profile/(.+)$ index.php?id=$1 [L,QSA,NC]