-1

I have a mean-stack website with html5mode enabled. In index.html, I have <base href="/1/" />. And I have the following nginx setting such that www.myweb.com/home becomes automatically www.myweb.com/1/home (www.myweb.com/js/abc.js remains www.myweb.com/js/abc.js):

location ~* ^/\b(?!1|stylesheets|js)\w+\b/? {
    rewrite .* /1$request_uri redirect;
}

Now, I have to add a special library, which unfortunately disables html5mode. So I am going to give up html5mode for the whole websites.

So now, I expect a rewrite rule such that

www.myweb.com/home ==> www.myweb.com/1/#/home
www.myweb.com/js/controller.js ==> www.myweb.com/js/controller.js
www.myweb.com/1/abc/def ==> www.myweb.com/1/#/abc/def
www.myweb.com/1/#/abc/def ==> www.myweb.com/1/#/abc/def

Does anyone know how to modify the above rewriting to enable this? Because my website is already online. I want to make sure the rules before changing the production...

Edit 1: I guess if I write

location ~* ^/\b(?!1|stylesheets|js)\w+\b/? {
    rewrite .* /1/#$request_uri redirect;
}

That will result in

www.myweb.com/home ==> www.myweb.com/1/#/home (correct)
www.myweb.com/js/controller.js ==> www.myweb.com/js/controller.js (correct)
www.myweb.com/1/abc/def ==> www.myweb.com/1/abc/def (wrong)
www.myweb.com/1/#/abc/def ==> www.myweb.com/1/#/abc/def (correct)
2
  • 1
    "Now, I have to add a special library, which unfortunately disables html5mode." That seems... odd. Why not fix that bit?
    – ceejayoz
    Jan 1, 2018 at 20:35
  • @ceejayoz It is really hard... You could take a look at questions I asked in stackoverflow... I am just trying different ways...
    – SoftTimur
    Jan 1, 2018 at 20:37

3 Answers 3

3

This answer is no longer valid in the context of the original question. I drew too quick conclusions on the subject.

After checking facts, I see that in https://www.rfc-editor.org/rfc/rfc7231 it is mentioned that HTTP redirects can contain fragment identifiers. This isn't incompatible with the description below though, as the information is passed only based on server side rules to the client.

I don't know if the actual redirect to destination URLs with fragment identifiers is possible in nginx though.

--- original answer ---

The fragment identifier doesn't appear at all in HTTP requests. It is purely a browser side concept, and you cannot do anything about it with server side software.

For example, when a page has a link to http://www.example.com/main/#part1, and user clicks the link, the browser sends an HTTP request to http://www.example.com/main/, and then scrolls down to the part marked with part1 identifier. This is the default behaviour.

You need to modify the actual HTML pages on your server to have the fragment identifiers you want.

1
  • Can't speak for nginx, but -- as an aside -- it is at least possible in Apache. The placement of the "#" in OP's unclear post, however, makes me wonder if that is even what he is talking about. I can't tell. Happy new year, Tero
    – Colt
    Jan 2, 2018 at 7:53
0

You can use a separate block for your /js path:

location ^~ /js {
    ... directives for this path ...
}

The ^~ tells nginx not to check regular expressions for this URL path. Further information on nginx location directives and their ordering can be found at http://nginx.org/en/docs/http/ngx_http_core_module.html#location.

0

I already use this redirect:

# Firts exclude all static files
location ~* (\/)(assets|css|fonts|img|js|lib|locales)(\/)(.*) {}

location ~ /templates {
    index  index.html index.htm;
    try_files $uri.html $uri $uri/ =404;
}

# point to your main file
location = /1 {
    try_files /index.html;
}

# finally make the redirect
# redirect for old html5Routing
#location ~ \/ {
#   rewrite ^ http://$host/1#$uri redirect;
#   break;
#}
# redirect for current html5Routing
location ~ \/ {
    rewrite ^ http://$host/1#!$uri redirect;
    break;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .