Solved

Rewrite Rule to Ignore/Strip Certain Parameter

Posted on 2013-11-21
4
356 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

22 Experts available now in Live!

Get 1:1 Help Now