【发布时间】:2011-07-31 05:13:01
【问题描述】:
我知道如何进行 URL 的重写,例如:
www.example.com/index.php?id=1&cat=3 到 www.example.com/1/3/(或其他)。我知道。
我不知道到底如何将所有页面中的整个链接更改为链接到漂亮的 URL。我网站的所有链接都是老式的 (<a href="index.php?id=1&cat=2">),而且有很多。
我在问如果用户点击 index.php?id=1,是否有人有想法或知道如何自动重定向到那个漂亮的 url。 (如果您更改 url 中的标题,几乎就像这个站点 Stackoverflow)。
所以我的推测是……
使用 .htaccess 读取 index.php?id=1&cat=2 来重写 index/1/3 自己再次解释(奇怪)
一个 php 文件来执行 htaccess 重写回原始的重定向...
结论:将<a href="index.php?id=1&.....">自动改为index/1/2
已解决
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
##################################
# This turns index.php?id=1&cat=2 into index/1/2 and then back 'transparent' into index.php?id=1&cat=2 if you have old fashioned
# links in your site and don't want to change them :)
# Avoid mod_rewrite infinite loops
# This is critical if you want to use this code
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
# Hard-rewrite ("[R]") to "friendly" URL.
# Needs RewriteCond to match original querystring.
# Uses "?" in target to remove original querystring,
# and "%n" backrefs to move its components.
# Target must be a full path as it's a hard-rewrite.
RewriteCond %{QUERY_STRING} ^id=(\d+)&cat=(\d+)$
RewriteRule ^index.php$ http://localhost/index/%1/%2/? [L,R]
# Soft-rewrite from "friendly" URL to "real" URL.
# Transparent to browser.
# Won't re-trigger the above rewrite, though I'm
# not really sure why! The order of the rules
# doesn't seem to make a difference.
RewriteRule ^index/(\d+)/(\d+)/$ index.php?id=$1&cat=$2 [L]
【问题讨论】:
标签: php mod-rewrite rewrite friendly-url