假设1和2确实是相同的 id,您可以将这些规则添加到文档根目录中的 htaccess 文件中:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ReewriteRule ^/?([^/]+)/([^/.]+)\.html$ /gallery.php?id=$2&title=$1 [L]
这使得当有人去时http://mydomain.com/foo/bar.html
,他们会在/gallery.php?id=bar&title=foo
.
为了更正您无法控制的所有外部链接以开始使用新的更好看的 URL,您也可以添加这些:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /gallery\.php\?id=([^&]+)&title=([^\ ]+)
RewriteRule ^ /%2/%1.html? [L,R=301]
这使得当有人放 时http://mydomain.com/gallery.php?id=123456&title=blah
,浏览器会被重定向到http://mydomain.com/blah/123456.html
,从而将浏览器地址栏中的地址更改为更好看的 URL。