开发者

Mod rewrite - Dynamic $_GET rewrite

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜