• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

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

I have created a .htaccess file at the following location:
http://specialopspaintball.com/shop/

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 http://shop.specialopspaintball.com/.

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: http://specialopspaintball.com/shop/imageviewer.asp?SKU=611+0002&IMAGE=b
Results in: http://shop.specialopspaintball.com/?SKU=611+0002&IMAGE=b

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 ^(.*)$ http://shop.specialopspaintball.com/ [L,R=301]

Open in new window

0
tlcable7
Asked:
tlcable7
  • 2
  • 2
1 Solution
 
caterham_wwwCommented:
- 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 ^(.*)$ http://shop.specialopspaintball.com/? [L,R=301]

Open in new window

0
 
tlcable7Author Commented:
Perfect!

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:
www.specialopspaintball.com/shop/search.asp?SEARCH=221+0022&x=0&y=0

End result:
http://shop.specialopspaintball.com/catalogsearch/result/?q=221+0022

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


RewriteCond %{QUERY_STRING} SEARCH
RewriteRule ^search.asp\?SEARCH=([^&]*)$&?.* http://shop.specialopspaintball.com/catalogsearch/result/?q=$1 [NC,L]

Open in new window

0
 
caterham_wwwCommented:
The queryString is not available in a rule-pattern, you've to use a condition:
RewriteCond %{QUERY_STRING} SEARCH=([^&]+)
RewriteRule ^search\.asp$ http://shop.specialopspaintball.com/catalogsearch/result/?q=%1 [R=301,NC,L]

Open in new window

0
 
tlcable7Author Commented:
Works great! Thank you very much.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now