Page anchor with mod_rewrite?

ⅰ亾dé卋堺 提交于 2019-12-11 04:43:00

问题


I'm trying to set up page anchors on a website that uses mod_rewrite (Apache2 running on Ubuntu Server 9.04).

My htaccess file looks like this:

RewriteEngine On
RewriteRule ^information.php/([A-Za-z0-9-]+)/?$ information.php?display=$1  [NC,NE]

If I was using regular URL's the query would look something like this: http://mydomain/information.php?display=faq#cost

I'm hoping to get something like this: http://mydomain/information/faq/cost

Is this possible? My understanding is that modrewrite ignores page anchors, and that the browser deals with it? I'm guessing that I can somehow use mod_rewrite to include the anchor information with the request, but I haven't been able to find anything documenting this and have been trying unsuccessfully to write it myself for hours.

Thanks!


回答1:


Actually, if you want the resulting URL to have an anchor, then yes, it's possible. Just don't forget that in Apache configs, # marks the start of a comment.

If what you want is like this - user enters

http://example.com/page/anchor
and gets redirected to
http://example.com/?p=page#anchor
- you would need to use 301 Redirect, or something like that, so it wouldn't be transparent to the user.

Conclusion: While it is possible to write such a redirect rule, it can't be done entirely server-side. So I think you could point /information/faq to /information.php?display=faq and then use URLs such as:

http://example.com/information/faq#foo
which are almost what you want, plus they don't mess up caching.

(Whoa, it's midnight already?)



来源:https://stackoverflow.com/questions/941721/page-anchor-with-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!