使用htaccess重定向和映射同一页面

时间:2014-04-21 07:32:22

标签: .htaccess

使用htaccess无法正常工作的同一页面的重定向和映射

前。 www.xyz.com?view=aa&param=bb - 将其重定向到www.xyz.com/aa/bb

然后将www.xyz.com/aa/bb映射到

www.xyz.com?view=aa&param=bb

以下是规则。

重定向

RewriteCond %{QUERY_STRING} ^view=([^&]*)&p=([0-9]+)&/(.*)$ 
RewriteRule ^insights/(.+?)\.html$ insights/$1/%1/%2-%3.html? [R=301,L] 

映射

RewriteRule ^insights/(.*)/(.*)/([0-9]+)-(.*)\.html$ /insights/$1.html?view=$2&p=$3&/$4 [L,QSA,NC]

2 个答案:

答案 0 :(得分:0)

将这两条规则放在.htaccess的顶部:

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?view=([^&]+)&param=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /?view=$1&param=$2 [L,QSA]

答案 1 :(得分:0)

这应该适用于你想要做的事情:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# Redirect /insights/anything.html?view=anything&p=anything&/anything
# To /insights/anything/anything-anything.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/([^.]+)\.html\?view=([^&\s]+)&p=([^&\s]+)&/([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3/%4-%5.html? [R=302,L]

# Internally forward /insights/anything/anything-anything.html
# To /insights/anything.html?view=anything&p=anything&/anything
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^-]+)-([^.]+).html$ /$1/$2.html?view=$3&p=$4&/$5 [L]

只有在您确认规则有效以避免浏览器缓存错误的重定向后,才能将R=302更改为R=301