.htaccess rewrite URL with question mark

Viewed 67

I have htaccess code given to me by a program called "Post Affiliate Pro" that looks like this

# Start Post Affiliate SEO Code----

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]

RewriteCond %{REQUEST_FILENAME} -d

RewriteRule .? - [S=4]

RewriteRule ^?coupon-code=([a-zA-Z0-9_-]+)\/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=$1 [R=301,L]

RewriteRule ^?coupon-code=([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=$1&a_bid=$2 [R=301,L]

RewriteRule ^?coupon-code=([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=$1&a_bid=$2&chan=$3 [R=301,L]

RewriteRule ^?coupon-code=([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=$1&a_bid=$2&chan=$3&data1=$4 [R=301,L]

RewriteRule ^?coupon-code=([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=$1&a_bid=$2&chan=$3&data1=$4&data2=$5 [R=301,L]

# End of Post Affiliate SEO Code

This breaks my site because the URL I am trying to redirect has a query string at the beginning, like so https://example.com/?coupon-code=PGZ

I know I need to modify it using QUERYSTRING somehow, but I don't know exactly what to do. Can anyone help me modify this htaccess code so it won't break my site?

1 Answers

With your shown attempts/samples, please try following Rules file in your .htaccess file. Please make sure to clear your browser cache before testing your URLs.

##Making RewriteEngine ON here.
RewriteEngine On
##For existing pages rules here..
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .? - [S=4]

##For handling 4 parameters handling rules here.
RewriteCond %{QUERY_STRING} ^coupon-code=([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ [NC]
RewriteRule ^/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=%1&a_bid=%2&chan=%3&data1=%4 [R=301,L]

##For handling 3 parameters handling rules here.    
RewriteCond %{QUERY_STRING} ^coupon-code=([\w-]+)/([\w-]+)/([\w-]+)/?$ [NC]
RewriteRule ^/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=%1&a_bid=%2&chan=%3 [R=301,L]

##For handling 2 parameters handling rules here.
RewriteCond %{QUERY_STRING} ^coupon-code=([\w-]+)/([\w-]+)/?$ [NC]
RewriteRule ^/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=%1&a_bid=%2 [R=301,L]

##For handling 1 parameters handling rules here.
RewriteCond %{QUERY_STRING} ^coupon-code=([\w-]+)/?$ [NC]
RewriteRule ^/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=%1 [R=301,L]
    
##For handling 5 parameters handling rules here.
RewriteCond %{QUERY_STRING} ^coupon-code=([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ [NC]
RewriteRule ^/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=%1&a_bid=%2&chan=%3&data1=%4&data2=%5 [R=301,L]

# End of Post Affiliate SEO Code
Related