Page anchor with mod_rewrite?

2019-07-30 14:47发布

问题:

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?)