问题描述
丑陋的网址 - example.com/folder_name/art.PHP?id=9&s=something
想成为 - example.com/folder_name/id/s
这是我的尝试
# external
RewriteCond %{THE_REQUEST} \s/+art\.PHP\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ art.PHP?id=$1&s=$2 [L]
不起作用
输入丑陋的网址 - 地址栏不变
输入漂亮的网址 - 出现错误 404
请帮忙
解决方法
您的规则还必须允许 folder_name
,因此请使用:
RewriteEngine On
# external
RewriteCond %{THE_REQUEST} \s/+(folder_name)/art\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
# internal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(folder_name)/([\w-]+)/([\w-]+)/?$ $1/art.php?id=$2&s=$3 [L,QSA,NC]
这假设 folder_name/
中没有其他 .htaccess。
如果该子文件夹中有 .htaccess,则使用此 .htaccess:
RewriteEngine On
RewriteBase /folder_name/
# external
RewriteCond %{THE_REQUEST} /art\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ %1/%2? [R=301,L]
# internal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ art.php?id=$1&s=$1 [L,QSA]