我希望使用以下两个参数更改网址:
https://www.example.com/single?name=Name-name&id=1
https://www.example.com/compilation?name=Name-name&id=1
https://www.example.com/album?name=Name-name&id=1
到
https://www.example.com/single/Name-name/1
https://www.example.com/compilation/Name-name/1
https://www.example.com/album/Name-name/1
该站点使用Apache 2.2.15托管在Centos 6.9上 我在Virtualhost - Directory
中有以下内容<Directory "/var/www/html/example">
Options FollowSymLinks
#Options SymLinksIfOwnerMatch
Options -Indexes
AllowOverride All
<IfModule rewrite_module>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
#check if not in admin dir
RewriteCond %{REQUEST_URI} !^/admin/
#1)externally redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]
#Rewrite index.php to /
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteRule ^(.*)/([0-9]+)$ single?name=$1&id=$2 [L]
RewriteRule ^(.*)/([0-9]+)$ compilation?name=$1&id=$2 [L]
RewriteRule ^(.*)/([0-9]+)$ album?name=$1&id=$2 [L]
</IfModule>
我不知道自己做错了什么,我尝试过不同的重写规则,有或者是whitout扩展名(single.php,compilation.php和album.php)但是他们都没有工作。我将不胜感激任何帮助
答案 0 :(得分:0)
更改您的最后3条规则:
RewriteRule ^single/([^/]+)/(\d+)/?$ single?name=$1&id=$2 [L]
RewriteRule ^compilation/([^/]+)/(\d+)/?$ compilation?name=$1&id=$2 [L]
RewriteRule ^album/([^/]+)/(\d+)/?$ album?name=$1&id=$2 [L]
将它们放在#1)externally redirect
...之前
如果他们使用php文件(带.php)改为:
RewriteRule ^single/([^/]+)/(\d+)/?$ single.php?name=$1&id=$2 [L]
RewriteRule ^compilation/([^/]+)/(\d+)/?$ compilation.php?name=$1&id=$2 [L]
RewriteRule ^album/([^/]+)/(\d+)/?$ album.php?name=$1&id=$2 [L]
要重定向到漂亮的链接,请使用(在其他人之前):
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+(single|compilation|album)(?:\.php)?\?name=([^\s&]+)&id=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L,NE]