Search code examples
.htaccessmod-rewritehttp-redirectdynamic-url

htaccess redirect using dynamic urls


I have an .htaccess file that does a lot of redirecting. A few of my redirects are not working in the mod_rewrite part. I want these redirects to work like this:

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

What I've tried so far:

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-type and airport are both variables that determines the dynamic content in the page to be loaded.


Solution

  • You will need 2 redirect rules:

    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]