通过启用 mod_rewrite 和 .htaccess httpd.conf
,然后将此代码放在您.htaccess
的DOCUMENT_ROOT
目录下:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# externally redirect /viewgallery.php?cname=Colorado-Fall&pcaption=Touched-By-Light to
# /viewgallery/Colorado-Fall/Touched-By-Light to
# RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(viewgallery)\.php\?cname=([^&]+)&pcaption=([^&\s]+) [NC]
# RewriteRule ^ /%1/%2/%3? [L,R=301,NE]
# internally forward /viewgallery/Colorado-Fall/Touched-By-Light to
# /viewgallery.php?cname=Colorado-Fall&pcaption=Touched-By-Light
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?cname=$2&pcaption=$3 [L,QSA]
解释:
- 规则 1 用于外部重定向以使您的 URL 更漂亮
- 它在 %1、%2、%3 中捕获“viewgallery”和参数 cname 和 pcaption 的值
- 此后,这些捕获的变量用于创建漂亮的 URL
/viewgallery/%2/%3
- 规则 2 适用于内部前锋
- 从 URL:
/viewgallery/Colorado-Fall/Touched-By-Light
它捕获 3 个变量:“viewgallery”、“Colorado-Fall”和“Touched-By-Light”到 $1、$2、$3
- 捕获的变量用于构造真实的 URL:
/viewgallery.php?cname=$2&pcaption=$3
更新:
# /viewgallery.php?cname=Colorado-Fall&pcaption=Poked to /photos/Colorado-Fall/Poked.jpg
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+viewgallery\.php\?cname=([^&])+&pcaption=([^&\s]+) [NC]
RewriteRule ^ /photos/%1/%2.jpg? [R=301,L]
RewriteRule ^photos/([^/]+)/([^.]+)\.jpg$ /viewgallery.php?cname=$1&pcaption=$2 [QSA,L,NC]