0
votes

I'm trying to redirect our old site from http to https and from a non-www to www at the same time. We have currently a lot of link that we have acquired over the years with the non www & http version of the site and I want to reduce the amount of redirects, since don't want to be diluted by "link-juice". We also have links from https// non-www and http//www :/

Currently the redirect schema looks like this:

http: //domain.com/oldpage.html
301 Moved Permanently
https: //domain.com/oldpage.html
301 Moved Permanently
https: //www.domain.com/newpage.html
200 OK

Is there a workaround to get one 301 redirect less? For example like this:
http//domain.com/oldpage.html -> to https://www.domain.com/newpage.html

3
Sure, that's entirely possible.ceejayoz

3 Answers

1
votes

This is what you want in your .htaccess

Only 1 redirect, regardless if it's insecure (http), apex (www-less) or both:

# canonical redirect http → https ( with optional www complection)
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^/?(.*) https://www.example.com/$1 [L,R=301]

# https:// apex →  https:// www.*
RewriteCond %{HTTP_HOST} ^(?!www\.)example.com [NC]
RewriteRule ^/?(.*) https://www.example.com/$1 [L,R=301]
0
votes

yes and it's easy to understand run regular expression over any URL of request that match the prefered form which is

https://www.

like in javascript /^https:\/\/www\..*$/.exec(url) if this returned non null value thats mean the url folow the https & www form

else if one of them vaiolats your rules like http or non www url or all of them parse the url and extract the path only then append it to your perfect url

var redirectURL = "https://www.yourDomain.com"+path;

And then send redirect request to this perfect form.

from this request:

http://yourDomain.com/something

redirect to new URL

https://www.yourDomain.com/something

just in one redirect response

0
votes
   NameVirtualHost *:80
   <VirtualHost *:80>
   ServerName example.com
   ServerAlias www.example.com
   RedirectPermanent / https://www.example.com
   </VirtualHost>

   <VirtualHost *:80>
   ServerName example.net
   ServerAlias www.example.net
   DocumentRoot /var/www/html/example.net
   RedirectMatch "old-page$" "https://www.example.com/new-page”
   </VirtualHost>

   <VirtualHost *:443>
   ServerName www.example.com
   ...
   </VirtualHost>

Ref: https://wiki.apache.org/httpd/RedirectSSL