redirection and mapping for same page using htacce

2019-09-15 03:42发布

redirection and mapping for same page using htaccess not working

Ex . www.xyz.com?view=aa&param=bb -- redirect it to www.xyz.com/aa/bb

Then map www.xyz.com/aa/bb to

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

below is the rules.

redirection

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

mapping

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

标签: .htaccess
2条回答
祖国的老花朵
2楼-- · 2019-09-15 04:24

Keep these 2 rules at top of your .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]
查看更多
三岁会撩人
3楼-- · 2019-09-15 04:45

This should work to what you're trying to do:

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]

Change R=302 to R=301 only after you have confirmed the rule is working to avoid your browser from caching wrong redirects.

查看更多
登录 后发表回答