简体   繁体   中英

Is it possible to rewrite to a URL with a fragment in it in nginx?

Eg

rewrite ^/page /#page;

I'm not sure if this would even be possible, due to the nature of fragments (never sent to the server etc). But since I'm rewriting TO one, rather than FROM one, I think it should work?

In which case how do I encode/escape that hash sign so that it doesn't start a comment..?

Thanks!

As you know, the fragment is used by the browser and is not sent to the server.

But you can use nginx to rewrite a request into a new URI containing a fragment, but it only makes sense if the new URI is sent to the browser, ie by using an HTTP 3xx response.

The rewrite directive will generate an HTTP 3xx response when the redirect (302) or permanent (301) flag is provided (or the replacement string starts with a scheme - see this document for more).

For example:

rewrite ^/page /#page redirect;

Adding quotation marks, could help. I tried in Docker Nginx latest image (1.17.4)

rewrite ^/page "/#page" redirect;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM