Solved

Rewrite Rule to Ignore/Strip Certain Parameter

Posted on 2013-11-21
4
361 Views
Last Modified: 2013-12-02
I am doing a number of URL rewrites and I have a some URL patterns where I would rather strip one of the params or ignore it when running the rule.

for example:
http://www.domain.com/page.php?name=Something Really & Bad&id=12
but then I might have another link for the same page that comes in as
http://www.domain.com/page.php?id=12

I would rather write one rule for both cases however right now I am stuck writing two rules for these redirects.

RewriteCond %{QUERY_STRING} ^(.*&)id=11$ [NC]
RewriteRule ^page\.php$ http://www.domain.com/pagename.html? [R=301,L]
RewriteCond %{QUERY_STRING} ^(.*&)id=18$ [NC]
RewriteRule ^page\.php$ http://www.domain.com/pagename2.html? [R=301,L]
RewriteCond %{QUERY_STRING} ^id=11$ [NC]
RewriteRule ^page\.php$ http://www.domain.com/pagename.html? [R=301,L]
RewriteCond %{QUERY_STRING} ^id=18$ [NC]
RewriteRule ^page\.php$ http://www.domain.com/pagename2.html? [R=301,L]

Open in new window


That takes care of both cases I would rather wrap that into one rule per page rather then two separate rules.

Any thoughts or magic?

Last thing is would you put this in .htaccess or httpd.conf, my only concern with htaccess is the # of rules that I will end up with being around 500 and it might be better in httpd.conf as I understand htaccess gets read on each page load where as httpd.conf is read into memory.
0
Comment
Question by:iceman19330
  • 2
4 Comments
 
LVL 50

Expert Comment

by:Steve Bink
ID: 39668020
Try this:
RewriteEngine On

RewriteCond %{QUERY_STRING} (.*&?)foo=bar&?(.*) [NC]
RewriteRule /?(.*) $1?%1%2 [NC]

Open in new window

0
 
LVL 14

Accepted Solution

by:
jb1dev earned 500 total points
ID: 39668022
Does this work for you?

    RewriteCond %{QUERY_STRING} ^(.*&)?id=11$ [NC]
    RewriteRule ^page\.php$ http://www.domain.com/pagename.html? [R=301,L]
    RewriteCond %{QUERY_STRING} ^(.*&)?id=18$ [NC]
    RewriteRule ^page\.php$ http://www.domain.com/pagename2.html? [R=301,L]

Open in new window

0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 39668027
Even better:
RewriteCond %{QUERY_STRING} (.*&?)id=(11|18)&?(.*) [NC]
RewriteRule /?(.*) $1?%1%3[NC]

Open in new window


You can continue to expand that list, or even replace with a RewriteMap call if you have a lot of them.
0
 

Author Closing Comment

by:iceman19330
ID: 39690080
This is the one I used and seems to do the trick.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

815 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

15 Experts available now in Live!

Get 1:1 Help Now