Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Rewrite Rules htaccess combining 2 redirects


douglaswalker

Recommended Posts

Hi there

i am looking for  some help with how to combine parts of the below htaccess to make it more efficient

At the moment any traffic to my site is redirected to https ... www,  nonwww, etc

So that all works fine. At the  end of the code it redirects index.php to home

All works just fine but  if  someone comes to index.php from nonwww or www or http it first redirects to https://www.mydomain/index.php and redirects again to https://www.mydomain

i am trying to find a way to avoid the second hop.

Doug :->

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_USER_AGENT} !facebookexternalhit/[0-9]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv
$RewriteCond %{REQUEST_URI} !^/\.known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?
$RewriteRule  ^(.*)$  https://www.domain/$1  [R=301,L]
RewriteCond  %{HTTP_HOST}  !^www\.  [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv
$RewriteCond %{REQUEST_URI} !^/\.known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?
$RewriteRule  ^(.*)$  https://www.domain/$1  [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://www.domain/ [R=301,L]

 

Link to comment
Share on other sites

You look to have an over complicated listing with several duplication!

So what is home? And why redirect index.php to home? Index.php should be your home unless you have other stuff in your root! Only you will know the answer to that.

Remove all duplication and simply the .htaccess file as much as possible.

You only need the minimum required in it.

Follow the guidelines in this guide.

https://httpd.apache.org/docs/2.4/rewrite/remapping.html

or look at this,

 

 

Link to comment
Share on other sites

Thank-you

I can see they are a similar idea and have compared them side to side but translating them to my code because of the

$RewriteCond %{REQUEST_URI} !^/\.known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?

has me very confused

I am also unsure as to why the above code appears twice.

That part is something me host did to implement https

 

 

Link to comment
Share on other sites

@douglaswalker

You've got more in there than just your redirects ...

1) The line you mentioned above has something to do with your SSL certificate (the word 'Comodo' was my first clue). I also Googled 'pki-validation' :

https://www.google.com/search?q=pki-validation

2) You also have something in there with 'facebookexternalhit' ...

3) Then there are the lines with 'cpaneldcv' in them ...

I suggest you ask your host what's going on in there ...

HTH

M

Link to comment
Share on other sites

@douglaswalkerDon't change the lines with cpanel and comodo in them. They are added by cpanel and should not be edited. regarding the others, you should place the more specific first. For example, you have the check for www redirecting to https then you have the check for index.php redirecting to https. That's two redirects when the index.php page is accessed. If you switch those, the redirect for index.php will cause a switch to www so the check for www will be skipped. Likewise, the check for https should come last since the rules for the others will switch to https.

Support Links:

For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc.

Get the latest versions of my addons

Recommended SEO Addons

Link to comment
Share on other sites

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_USER_AGENT} !facebookexternalhit/[0-9]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule  ^(.*)$  https://www.domain.com.au/$1  [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://www.domain.com.au/ [R=301,L]

RewriteCond  %{HTTP_HOST}  !^www\.  [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule  ^(.*)$  https://www.domain.com.au/$1  [R=301,L]
Link to comment
Share on other sites

The cpanel and comodo commands are placed there when the site uses the AutoSSL. This thread explains it a little.

As for the rules, I don't think you need the RewriteCond %{HTTPS} !on at all since cpanel will force ssl when the AutoSSL is used. But be sure to test it if you remove it. If you leave it in, it should go at the bottom since the others will switch to https when used.

The RewriteCond %{HTTP_USER_AGENT} !facebookexternalhit/[0-9] line is checking every connection and redirecting to https and www if it isn't a facebok bot so that should be below the index.php check but above the www and ssl checks.

 

Support Links:

For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc.

Get the latest versions of my addons

Recommended SEO Addons

Link to comment
Share on other sites

@Jack_mcs

How is this looking before i try it live

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule  ^(.*)$  https://www.domain.com.au/$1  [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://www.domain.com.au/ [R=301,L]
RewriteCond %{HTTP_USER_AGENT} !facebookexternalhit/[0-9]

RewriteCond  %{HTTP_HOST}  !^www\.  [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule  ^(.*)$  https://www.domain.com.au/$1  [R=301,L]

 

Link to comment
Share on other sites

@Jack_mcs

I think i have it ..after having  a good look at the link you offered on the cpanel forum

RewriteEngine On


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^index\.php$ https://www.domain.com.au/ [R=301,L]

RewriteCond %{HTTP_USER_AGENT} !facebookexternalhit/[0-9]
RewriteCond  %{HTTP_HOST}  !^www\.  [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule  ^(.*)$  https://www.domain.com.au/$1  [R=301,L]

 

Link to comment
Share on other sites

@Jack_mcs

Still testing here

The code below fails to redirect to https when  www.domain.com.au is entered

it redirects for non-www

RewriteEngine On


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^index\.php$ https://www.domain.com.au/ [R=301,L]

RewriteCond %{HTTP_USER_AGENT} !facebookexternalhit/[0-9]
RewriteCond  %{HTTP_HOST}  !^www\.  [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule  ^(.*)$  https://www.domain.com.au/$1  [R=301,L]

 

Link to comment
Share on other sites

All you should need is this to redirect to https, the other stuff your host is adding! you need to ask them why?

RewriteEngine On

RewriteCond %{HTTPS} off [OR]

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

With rewrite rules there are many ways to skin the same cat! it’s best to keep it simple and short!

 

Link to comment
Share on other sites

The code the host adds is from Cpanel for https

Jack helpfully linked to the answer about it above.

I understand I only need something like the code you posted but i also need to redirect index.php to https://mysite.com.au  as well as I explained above.

For some reason the code I posted is failing to swap when www is used... all other things are working

 

 

Link to comment
Share on other sites

I think we are talking at cross purposes

this code

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

redirects to https

 

This code tells the search engines that index.php and https://www.domain.com.au  are the same page

 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^index\.php$ https://www.domain.com.au/ [R=301,L]

 

Link to comment
Share on other sites

In my experience with AutoSSL, the ".cpaneldcv" and "pki-validation" lines may be safely removed if you can guarantee that for that Rewrite section, they will never be triggered (i.e., the REQUEST_URI does NOT match either). Generally this means that you are testing for a specific REQUEST_URI yourself, so both will always be "true" (i.e., not matching those patterns). If in doubt, leave them in -- they're harmless except they eat a few more CPU cycles.

As for http::// www.domain.com.au  failing to redirect to https, obviously it's because you removed the %{HTTPS} check. Try the following to do a rewrite if non-www and/or http:

RewriteCond  %{HTTPS}  !on  [OR]
RewriteCond  %{HTTP_HOST}  !^www\.  [NC]

That would have to come at the top of a RewriteRule's section, as there is no precedence (hierarchy) between OR and the default AND flags. You can put the .cpaneldcv and pki-validation lines after those.

So long as you are careful to fully redirect to both https and www in each section, you should not have a problem with multiple 301 redirects angering the SEO gods. People get into trouble when they have something like "301 redirect non-www to http://www" and then "301 redirect http to https". That creates two 301's.

Link to comment
Share on other sites

@MrPhil

Just before you posted I ended up with this (see below) and realised I had removed the important bit (slaps head DOH)

I have tested all the permutations and I only get one 301 redirect for each option with or without index.php

So very happy...  Could you explain a little more about removing the pki stuff .... how would they be triggered.. also the path they follow goes to an empty file also.

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^index\.php$ https://www.site.com.au/ [R=301,L]

RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_USER_AGENT} !facebookexternalhit/[0-9]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule  ^(.*)$ https://www.site.com.au/$1 [R=301,L]

RewriteCond %{HTTP_HOST}  !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule  ^(.*)$ https://www.site.com.au/$1 [R=301,L]

 

Link to comment
Share on other sites

5 hours ago, douglaswalker said:

The code below fails to redirect to https when  www.domain.com.au is entered

Then you probably needs to add the check for https back in. That thread indicates otherwise but I've never tested it so apparently that is not the case.

Support Links:

For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc.

Get the latest versions of my addons

Recommended SEO Addons

Link to comment
Share on other sites

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$

These are ANDed into the chain of RewriteCond's. Note the ! at the beginning of the regex pattern, which means that if the requested URI (the part after the domain, and before any Query String) does match, the RewriteRule will not be executed. The first one is looking for one or more digits followed by a period followed by one or more other characters, and ending with .cpaneldcv. If the URI matches that, the rewrite will not be done. The second one is looking for a URI of .well-known/pki-validation/ followed by 32 hex digits (I presume a key unique to your site) followed by .txt, and possibly ending with a space Comodo space DCV. Again, if a match, the rewrite is not done. What these things actually do, I have no idea, except that an incoming URI matching either prevents your rewrites from doing anything -- presumably the URL goes unaltered to some special place (these are real addresses on your server) and does some sort of magic there. Maybe that "empty file" you mentioned is some sort of "proof of purchase" or something. Just leave it alone.

My point about sometimes being able to remove them is that if you are (among other things) testing for a  specific URI (%{REQUEST_URI}) before doing a rewrite, neither of those patterns should match, both RewriteCond's should therefore be true (!false), and they're essentially no-ops.

Concerning the three rewrites you have there, the first one is to remove any explicit index.php, the second one rewrites if not https and is not a FaceBook hit agent, and the third one rewrites if the domain is not the desired www. Do you know what this FB agent thing is supposed to do, and why it appears in only one rewrite? It appears that it would prevent http from being redirected to https (the second rewrite), if it's not /index.php or site.com.au. If it weren't for the FB agent check, you could combine the second and third rewrites:

RewriteCond %{HTTPS} !on  [OR]
RewriteCond %{HTTP_HOST}  !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule  ^(.*)$ https://www.site.com.au/$1 [R=301,L]

If it's working OK, perhaps the best thing is to leave it alone. :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...