2

I had a rewrite from file.js?c=123 to file.js.php?c=123 but query params were downgrading my SEO evaluation. Actually, some proxy CDN do not cache query params.

server {
    location ~ \startscript.js$ {
        rewrite (.*) $1.php last;
    }
}

SO, I would like to rewrite file.123.js to file.js.php?c=123. Is that possible?

2
  • 1
    "some proxy CDN do not cache query params" - was that something you read, or something you experienced? This shouldn't make any difference in terms of "SEO"?
    – MrWhite
    Nov 2, 2018 at 16:30
  • @MrWhite my SEO has decreased after apply url to static with querystring. Actually, GTMetrix has warning related to it.
    – fiskolin
    Nov 6, 2018 at 10:52

1 Answer 1

2
+50
location ~ \startscript.js$ {

In regex the \s at the start of your uri is a shorthand character class that matches a single space (or "whitespace character"). So this is unlikely to have matched anything. However, you should backslash escape the literal dot before the file extension.

I assume "file" in file.js could be any file, it's not always "startscript"?

Try the following instead (UPDATED):

server {
    location ~ \.js$ {
        rewrite (.+)\.(\d+)\.js$ $1.js.php?c=$2 last;
    }
}

However, as I mentioned in comments, this makes no difference with regards to SEO (search engines handle the query string just fine - always have). In the past, it was possibly a usability issue, as some proxy servers did not cache based on the query string - so users might not have seen the correct content (if they were behind a proxy that behaved this way) - but this is historical AFAIK.

See also my answer to the following question:


... this is throwing 404 error, Eg: domain.com/startscript.82b4527379bd2540f79f532950c7b96b.js

The above example specifically matches digits only (as in your initial example). What you have there looks like a 32 char hex value (hash?). Try the following instead (UPDATED):

rewrite "(.+)\.([\da-f]{32})\.js$" $1.js.php?c=$2 last;

Or use the the more generic \w+, as @devnull suggested in comments, to match any "word" character (ie. 0-9, a-z, A-Z and _). Although with regex, it's generally advisable to be as specific as possible, so if it's always a 32 char hex then match a 32 char hex.

7
  • Hmm, this is throwing 404 error, Eg: domain.com/startscript.82b4527379bd2540f79f532950c7b96b.js
    – fiskolin
    Nov 6, 2018 at 13:57
  • 1
    @fiskolin, your question only showed integers. The example URL you just posted uses integers and letters. Try changing (\d+) to (\w+) in MrWhite's answer and that should match against the sample you just provided.
    – devnull
    Nov 6, 2018 at 14:47
  • I've updated my answer.
    – MrWhite
    Nov 6, 2018 at 15:00
  • 1
    @MrWhite, first match on location location ~ \.js$ w/ rewrite (.+)\.(\d+)\.js$ $1.php?c=$2 last; isn't working (throwing 404 error). Second sol. rewrite (.+)\.([\da-f]{32})\.js$ $1.php?c=$2 last; is throwing an error on nginx conf file nginx: [emerg] directive "rewrite" is not terminated by ";". Tested on nginx version: nginx/1.10.2.
    – fiskolin
    Nov 7, 2018 at 17:32
  • Ah yes, sorry, I'd missed the .js extension from the target URL. The seond solution also needs quotes around the regex, since it contains }. I've updated my answer.
    – MrWhite
    Nov 7, 2018 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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