user2693302 user2693302 - 4 years ago 182
Apache Configuration Question

301 Rewrite Rule Not working

We want to redirect Below mentioned URL.

From URL:

www.example.com/itemLevelFilterPage.action?keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0


To URL :
www.example.com/4G


We wrote 301 rules in apache configuration as below.

RewriteEngine on
RewriteRule ^/itemLevelFilterPage.action?keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0 /4G [L,R=301]


But redirection is not working as expected. Any Suggestion will be highly appreciated.

Answer Source

mod_rewrite will not look at query strings as part of its matching, unless you specifically ask it to:

RewriteCond %{REQUEST_URI}  ^/itemLevelFilterPage\.action$
RewriteCond %{QUERY_STRING} ^keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0$
RewriteRule (.*)            http://www.example.com/4G?  [R=301,L]

The first condition makes sure we're looking at the right page, the second checks the query string, and the rule will rewrite the URL to the desired one.

The trailing question mark on the rule ensures the old query string is removed. If you're on Apache 2.4 the query string discard flag is available:

RewriteRule (.*)            http://www.example.com/4G  [R=301,L,QSD]

More examples here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download