简体   繁体   中英

Using Apache mod_rewrite to move query parameters after /#

I need Apache to rewrite URLs by moving the HTTP query string from

/?foo=bar

to

/#/?foo=bar

so that those query parameters can be picked up by Angular's $location.search() .

I'd like this to work only on for the root path, / , and not on any other paths such as /hello .

I have the following Location block on my Apache VirtualHost config:

<Location />
  RewriteEngine on
  # Put a /# in front of the query string
  RewriteCond "%{QUERY_STRING}" "^(.*)"
  RewriteRule "/" "/#/?%1" [R=302,NE]
</Location>

which works for / but it also rewrites:

/hello?foo=bar

to

/#/?foo=bar

ie it drops the path /hello from the rewritten destination. I'd like Apache to rewrite /hello?foo=bar to /hello/#/?foo=bar

Using Angular html5 mode isn't possible because I have to support IE9 and under.

Can anyone help me with a better RewriteRule that only works for / ?

Thanks!

This seems to have done the trick:

<Location />
  RewriteEngine on
  # Put a /# in front of the query string
  RewriteCond %{REQUEST_URI} ^/$
  RewriteCond "%{QUERY_STRING}" "^(.+)"
  RewriteRule "/" "/#/?%1" [R=302,NE]
</Location>

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