Mod_Rewrite:将normal / url转换为/#!/ url

时间:2012-01-08 11:52:08

标签: ajax .htaccess mod-rewrite hash server-side

我有以下两个网址案例......

  1. http://url.com/one
  2. http://url.com/category/some
  3. 如果调用这些网址,我希望mod_rewrite调用...

    1. http://url.com/#!/one
    2. http://url.com/#!/category/some
    3. 我该怎么做?

1 个答案:

答案 0 :(得分:2)

这样的东西?

# not existing file (images, css, etc)
RewriteCond %{REQUEST_FILENAME} !-f
# no query parameters
RewriteCond %{QUERY_STRING} =""
# not /
RewriteCond %{REQUEST_URI} !^/$
# external redirect and pass along query string and uri as fragment
# i guess this must be an external redirect as the server side should
# not see the fragment, R=redirect, NE=dont escape #, L=last rule
RewriteRule ^(.*)$ /#!/$1 [R,NE,L]

# same but with query parameter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ /?%{QUERY_STRING}#!/$1 [R,NE,L]  

但我不确定这是不是好主意。也许你应该在应用程序逻辑或客户端脚本中进行重定向。