0

I have redirected my site from http to https. I want to allow the useragent XYZ and XYZ whatever to enter both http and https versions.

Now the vhost looks like this:

server {
        listen 80;
        server_name example.com www.example.com;
        rewrite  ^/(.*)$  https://www.example.com/$1 permanent;
}

How can I edit it, in order to allow agents that contain XYZ to see both HTTP and HTTPS? In order words... how to disable the redirect for such agents?

I tried

if ($http_user_agent ~* "^XYZ$") {
   rewrite ^/(.*)$ https://www.example.com/$1 permanent;
}

But this seems to redirect only XYZ. But I need to disable the redirect if XYZ, not strict XYZ, but containing XYZ...

1
  • I added what I tried
    – Pikk
    Jul 25, 2017 at 13:55

1 Answer 1

5

With ~* you are attempting to loosely match the string, but your regex is ^XYZ$ which says match only if it starts with XYZ and only if it ends in XYZ. That's not loose at all. You are kind of contradicting yourself there.

See documentation here: http://nginx.org/en/docs/http/ngx_http_core_module.html#location

You probably want to adjust your regex to allow for more variations. From your question it's hard for us to know what you would like to match though. Possibly something like:

if ($http_user_agent ~* ".*XYZ.*") {
   rewrite ^/(.*)$ https://www.example.com/$1 permanent;
}
7
  • I have HTTP redirection to HTTPS. I want to add a condition that will allow Facebook crawler to see both http and https (so to disable the redirect for FB). The FB agent has several names, but all of them contain facebookexternalhit/1.1
    – Pikk
    Jul 26, 2017 at 14:01
  • Advanced crawlers like Google and Facebook don't want you to modify application behaviour just for them. You basically want to show them something you will never show their trusted users. I wouldn't bother.
    – JayMcTee
    Jul 26, 2017 at 14:13
  • No, I disagree. FB developers suggest it. developers.facebook.com/docs/plugins/faqs#faq_1149655968420144 "This also requires that the old URL still renders a document with Open Graph tags and returns a HTTP 200 response, at least when loaded by Facebook's crawler. If you want other clients to redirect when they visit the URL, you must send your 301 HTTP response to all non-Facebook crawler clients. The old URL should contain its own og:url tag that points to itself. You can learn how to recognize Facebook's crawler in our Sharing Best Practices Guide."
    – Pikk
    Jul 26, 2017 at 14:29
  • That snippet doesn't mean you can't serve them https results with status 200.
    – JayMcTee
    Jul 26, 2017 at 14:30
  • If I want to transfer likes and shares that where given when the site was still http, then I need to allow the crawler to still see the old page.
    – Pikk
    Jul 26, 2017 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.