我知道如何进行 URL 重写,例如:www.example.com/index.php?id=1&cat=3
to www.example.com/1/3/
(管他呢)。我知道。
我不知道到底如何更改所有页面中的整个链接以链接到漂亮的 URL。我网站的所有链接都是老式的(<a href="index.php?id=1&cat=2">
)并且有很多。
我问是否有人有想法或知道如何在用户单击index.php?id=1 时自动重定向到该漂亮的网址。 (如果你更改网址中的标题,几乎就像这个网站 Stackoverflow)。
所以我的推测是...
使用.htaccess读取index.php?id=1&cat=2来重写自己再次解释的index/1/3(奇怪)
一个 php 文件来执行 htaccess 重写回原始的重定向...
结论:改变<a href="index.php?id=1&.....">
自动到index/1/2
SOLVED
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]
RewriteEngine on
# Prevents browser looping, which does seem
# to occur in some specific scenarios. Can't
# explain the mechanics of this problem in
# detail, but there we go.
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://example.com/index/%1/%2/? [L,R]
# Soft-rewrite from "friendly" URL to "real" URL.
# Transparent to browser.
RewriteRule ^index/(\d+)/(\d+)/$ /index.php?id=$1&cat=$2
当然,理想情况下,您只需修复链接,然后只需要软重写。 :)
Tested with Apache/2.2.3. I think I made up the terms "hard-rewrite" and "soft-rewrite".
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)