htaccess使用动态网址重定向

时间:2015-09-07 16:17:10

标签: .htaccess mod-rewrite redirect dynamic-url

我有一个.htaccess文件,可以进行大量的重定向。我的一些重定向不适用于mod_rewrite部分。我希望这些重定向能够像这样工作:

package_listing.php?package-type=fly-snooze-cruise => /fly-snooze-cruise
package_listing.php?package-type=fly-snooze-cruise&airport=orlando-airport => /fly-snooze-cruise/orlando-airport
package_listing.php?package-type=fly-snooze-cruise&airport=orlando-sanford-airport => /fly-snooze-cruise/orlando-sanford-airport
package_listing.php?package-type=fly-snooze-cruise&airport=melbourne-airport => /fly-snooze-cruise/melbourne-airport
package_listing.php?package-type=snooze-park-cruise => /snooze-park-cruise

到目前为止我尝试过:

RewriteRule ^package_listing.php?package-type=(.*)$ /$1 [L,R=301]
RewriteRule ^package_listing.php?package-type=(.*)$&airport=(.*)$ /$1/$2 [L,R=301]


RewriteRule ^package_listing.php?package-type=([A-Za-z/]*)$ /$1 [L,R=301]
RewriteRule ^package_listing.php?package-type=([A-Za-z/]*)$&airport=([A-Za-z/]*)$ /$1/$2 [L,R=301]


RewriteRule ^package_listing.php?package-type=snooze-park-cruise$ /snooze-park-cruise [L,R=301]
RewriteRule ^package_listing.php?package-type=fly-snooze-cruise$ /fly-snooze-cruise [L,R=301]
RewriteRule ^package_listing.php?package-type=fly-snooze-cruise&airport=orlando-airport$ /fly-snooze-cruise/orlando-airport [L,R=301]
RewriteRule ^package_listing.php?package-type=fly-snooze-cruise&airport=orlando-sanford-airport$ /fly-snooze-cruise/orlando-sanford-airport [L,R=301]
RewriteRule ^package_listing.php?package-type=fly-snooze-cruise&airport=melbourne-airport$ /fly-snooze-cruise/melbourne-airport [L,R=301]

package-typeairport都是确定要加载的页面中的动态内容的变量。

1 个答案:

答案 0 :(得分:1)

您需要2个重定向规则:

RewriteEngine On

RewriteCond %{THE_REQUEST} /package_listing\.php\?package-type=([^\s&]+)\s [NC]
RewriteRule ^ /%1? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /package_listing\.php\?package-type=([^\s&]+)&airport=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]