且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP .htaccess->漂亮的网址(反过来)

更新时间:2022-01-26 00:24:06

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

当然,理想情况下,只需修复链接,然后只需要软重写即可. :)

Of course, ideally, you'd just fix your links, and then you'd only require the soft-rewrite. :)

经过Apache/2.2.3测试.我想我把硬重写"和软重写"这两个术语组成了.