与查询字符串的URL重写问题重写、字符串、问题、URL

2023-09-02 11:29:15 作者:糖糖猫

我试图改写如 http://www.url.com/blog网址/ P = 123 以 http://www.url.com/#blog/123 。我读过了的东西,发现你只能解析查询字符串中的RewriteCond所以我想是这样的:

I'm trying to rewrite urls like http://www.url.com/blog/?p=123 to http://www.url.com/#blog/123. I've read up on things and found that you can only parse the query string in the RewriteCond so I tried something like:

RewriteCond %{QUERY_STRING} ^p=([0-9]*)$
RewriteRule ^.*$ /#blog/%0 [NE,R]

当我尝试这个网址最终被改写为:

When I try this the urls end up being rewritten to:

http://www.url.com/#blog/p= 213?p = 213

任何想法如何正确地做到这一点?此外,有没有一种方法来添加一个额外的RewriteCond,检查了 REQUEST_URI 包含博客?

Any ideas how to do this properly? Also, is there a way to add an additional RewriteCond that checks that the REQUEST_URI contains blog?

推荐答案

您可以通过删除查询字符串完全做到这一点:

You can do this by removing the query string entirely:

RewriteCond %{QUERY_STRING} ^p=([0-9]*)$
RewriteRule ^.*$ /#blog/%1? [NE,R]

这应该给你:

http://www.url.com/#blog/213

如果您要检查如果URL中包含术语博客那么简单检查:

If you want to check if the URL contains the term "blog" then simply check:

RewriteCond %{REQUEST_URI} .*/blog/.*

要注意的是,你将不能够为您在象 http://www.url.com/#blog ,因为链接博客是很重要的,正如帕特里克,之后的所有不会被发送到服务器。

It is important to note that you will not be able to check for "blog" in links like http://www.url.com/#blog because, as noted by Patrick, everything after the # is not sent to the server.

查看mod_rewrite的的阿帕奇维基了解更多信息。

See the Apache wiki on mod_rewrite for more information.