Mod 重写 - 动态 $_GET 重写

发布于 2024-10-27 21:32:44 字数 630 浏览 1 评论 0原文

无论如何,是否使用 ModRewrite 来实现以下目的:

初始 URL: http://example.com/page?value=测试

重写的网址:http://example.com/page/value/test

初始 URL:http://example.com/page?value=test&fruit=apple

重写 URL:http://example.com/page/value/test? Fruit=apple

我需要这个规则是动态的,因为我不知道它将用于哪些页面,而且我还需要在 PHP 中保留 $_GET 变量。

谢谢

Is there anyway using ModRewrite to achieve the following:

Initial URL: http://example.com/page?value=test

Rewirted URL: http://example.com/page/value/test

OR

Initial URL: http://example.com/page?value=test&fruit=apple

Rewirted URL: http://example.com/page/value/test?fruit=apple

I need this rule to be dynamic as I don't know all the pages it is going to be used for and I also need to retain the $_GET variables in PHP.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无妨# 2024-11-03 21:32:44

如果有问题的 URL 存储在数据库中,并且需要重写,直到可以更新内容以反映新的 URL 模式,那么这个问题就有意义了。为了澄清问题,这将被视为重定向而不是重写,从而改善搜索引擎优化。然后,正如上面的注释所指出的,可以重写 URL 来为 PHP 提供正确的 URL 参数。如果这正是 James 的意图,下面的配置将会有所帮助:

### 301 redirect old query string URLs to pretty URLs
### This will help search engines index the new URLs, 
### not ones that are linked in content
### This is rather messy due to the 
### http://example.com/page/value/test?fruit=apple example
RewriteCond %{QUERY_STRING} value=([^&]*)
RewriteRule (.*) /page/value/%1 [R=301,E=rewrite:true]

RewriteCond %{ENV:rewrite} true
RewriteCond %{QUERY_STRING} !&
RewriteRule (.*) $1? [R=301,L]

RewriteCond %{ENV:rewrite} true
RewriteCond %{QUERY_STRING} &([^=]*)=(.*)$
RewriteRule (.*) $1?%1=%2 [R=301,L]

### Rewrite pretty urls with usable parameters
### [QSA] will maintain extra params such as &fruit=apple
RewriteRule /page/value/(.*) /page.php?value=$1 [QSA]

这很混乱,但我遇到过类似的情况,在内容可以更新之前,需要重定向数据库中存储的旧 URL。

希望这有帮助。

The question makes sense if the URLs in question are stored in the database, and need to be rewritten until the content can be updated to reflect the new URL pattern. Which, for clarification of the question, would be considered a redirect not rewrite, therefore improving SEO. Then, as the comments above point out, the URL could then be rewritten to provide PHP the correct URL parameters. If this what James intended, the config below will help out:

### 301 redirect old query string URLs to pretty URLs
### This will help search engines index the new URLs, 
### not ones that are linked in content
### This is rather messy due to the 
### http://example.com/page/value/test?fruit=apple example
RewriteCond %{QUERY_STRING} value=([^&]*)
RewriteRule (.*) /page/value/%1 [R=301,E=rewrite:true]

RewriteCond %{ENV:rewrite} true
RewriteCond %{QUERY_STRING} !&
RewriteRule (.*) $1? [R=301,L]

RewriteCond %{ENV:rewrite} true
RewriteCond %{QUERY_STRING} &([^=]*)=(.*)$
RewriteRule (.*) $1?%1=%2 [R=301,L]

### Rewrite pretty urls with usable parameters
### [QSA] will maintain extra params such as &fruit=apple
RewriteRule /page/value/(.*) /page.php?value=$1 [QSA]

This is messy, but I've run into similar situations where redirecting old URLs stored in the database was necessary until content could be updated.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文