0
votes

I am trying to configure apache to redirect to www and https on our server, we use an ecom package that uses a vlink script which resides in a cgi-bin folder.

The instructions to rewrite /cgi-bin/scriptname to / are:-

RewriteEngine   on

RewriteRule     ^$      /cgi-bin/scriptname/  [L,NS,PT]
RewriteRule     ^/$      /cgi-bin/scriptname/  [L,NS,PT]
RewriteRule     ^/index\.html$      /cgi-bin/scriptname/  [L,NS,PT]

RewriteCond     /home/scriptname/www/html/%{REQUEST_FILENAME} -f      [OR]
RewriteCond     /home/scriptname/www/html/%{REQUEST_FILENAME} -d
RewriteRule     ^(.+)$  -  [L,NS]

RewriteCond     /%{REQUEST_FILENAME}    !^.*/cgi-bin/scriptname.*$
RewriteRule     ^(.+)$  /cgi-bin/scriptname/$1  [L,NS,PT]

I have this in both the :80 and :443 vhost sections in the apache conf.

Now I want to make sure that all urls redirect to https and www so I added this into .htaccess

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

It seems to work but when I look at what happens using wget I see multiple 301 permanently moved, sometimes 3 or 4 depending on the url, some referring to /cgi-bin/scriptname

Is this correct, I was thinking I should just see a single 301 moved followed by a 200?

1

1 Answers

0
votes

I have come up with the following solution and would welcome comments, it seems to work, in the port 80 vhost section I have

#not www with cgi-bin    
RewriteCond     /%{REQUEST_FILENAME}    ^.*/cgi-bin/scriptname.*$
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^/cgi-bin/scriptname/(.*) https://www.example.com/$1 [L,R=301]  

#cgi-bin
RewriteCond     /%{REQUEST_FILENAME}    ^.*/cgi-bin/scriptname.*$
RewriteRule ^/cgi-bin/scriptname/(.*) https://www.example.com/$1 [L,R=301]

#not www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#not https
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

in the port 443 vhost section

#not www with cgi-bin    
RewriteCond     /%{REQUEST_FILENAME}    ^.*/cgi-bin/scriptname.*$
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^/cgi-bin/scriptname/(.*) https://www.example.com/$1 [L,R=301]  

#cgi-bin
RewriteCond     /%{REQUEST_FILENAME}    ^.*/cgi-bin/scriptname.*$
RewriteRule ^/cgi-bin/scriptname/(.*) https://www.example.com/$1 [L,R=301]

#not www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This seems to only do 1 301 redirect for each of the url combinations.