Nginx: Escaping # in url rewrite

I have a JavaScript MVC application that needs to support Facebook sharing, which means it needs to support unique HTML OG meta tags.

I am doing an Nginx rewrite that will detect the Facebook crawler so that the server configures a custom version of the application with the appropriate OG tag for that section, but Apache ignores everything after the # sign (since the server side has to do the browser function since then.) I would like to avoid "# "in my rewrite, but not sure how to do it in Nginx:

location / {
  if ($http_user_agent ~* 'facebookexternalhit') {
    rewrite ^(.*)$ /og.php?url=http://$host$uri;
  root /var/www/html/;


Thanks for watching!


source to share

1 answer

You can't or you shouldn't. If your browser has the URL-address, for example http://www.example.tld/site.html#anchor

, then your browser request will consist only of non-anchor parts: http://www.example.tld/site.html

. After receiving the content, the browser will look for a named anchor with a name anchor

and scroll the page so that its content is visible.

The nginx value will never see the symbol #


If, on the other hand, the website contains a link c #

that is part of the URL path (and this is quite rare), then it needs to be escaped with the usual URL escaping %xx

c xx

is the hexadecimal number of that chacter - %23

in the case #




All Articles