How do handle URL redirects via .htaccess with multiple different scenarios.

Posted on 2008-11-11
Last Modified: 2012-05-05
I have created a .htaccess file at the following location:

I need calls to the pages: category.asp, parentCategory.asp, and product_detail.asp to be redirected to the same file name with .php as extension instead. All other requests should be sent to

Q1: As my code is now, the first three rules seem to be ignored, and the fourth one always executes. How can I make the first three take precedence?

Q2: When the fourth rewrite happens, it appends the query string to the redirect. How can I remove that?
Example link:
Results in:

Thank you,

Travis Cable
RewriteEngine on

RewriteRule ^category.asp /shop/category.php [NC]

RewriteRule ^parentCategory.asp /shop/parentCategory.php [NC]

RewriteRule ^product_detail.asp /shop/product_detail.php [NC]

RewriteRule ^(.*)$ [L,R=301]

Open in new window

Question by:tlcable7
    LVL 27

    Accepted Solution

    - set L-Flags
    - exclude your PHP files from your last rule

    > it appends the query string to the redirect. How can I remove that?

    Add a question mark:
    RewriteEngine on
    RewriteRule ^(category|parentCategory|product_detail)\.asp$ /shop/$1.php [NC,L]
    RewriteCond $1 !^((parent)?category|product_detail)\.php$ [NC]
    RewriteRule ^(.*)$ [L,R=301]

    Open in new window


    Author Comment


    Could you also help me get one more working. I have a search in the query string that I need redirected as well.

    Example URL:

    End result:

    I have attache my code so far, which does not work.

    RewriteCond %{QUERY_STRING} SEARCH
    RewriteRule ^search.asp\?SEARCH=([^&]*)$&?.*$1 [NC,L]

    Open in new window

    LVL 27

    Expert Comment

    The queryString is not available in a rule-pattern, you've to use a condition:
    RewriteCond %{QUERY_STRING} SEARCH=([^&]+)
    RewriteRule ^search\.asp$ [R=301,NC,L]

    Open in new window


    Author Comment

    Works great! Thank you very much.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
    If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now