The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 23, 2019, 06:48:59 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Conditional Apache Regex  (Read 3011 times)
DangerMouse
Expert
****
Offline Offline

Posts: 244



View Profile
« on: April 09, 2010, 12:44:14 AM »

Hello all,

Is there a way to write a conditional regex statement for a .htaccess file that strips only appends specific query paramaters to a new URL? i.e. redirect to x URL but append paramaters x y z from original URL, ignore all others.

Cheers,

Steve
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: April 09, 2010, 01:03:16 AM »

Yes, there is.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
DangerMouse
Expert
****
Offline Offline

Posts: 244



View Profile
« Reply #2 on: April 09, 2010, 02:15:04 AM »

 Grin Any chance you show me an example please Perk?

I got a copy of the book you recommended on regex sometime ago - and great though it is I still hate them!

Steve
Logged
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #3 on: April 09, 2010, 07:21:05 AM »

You capture the params with () and then backreference them. $1 is the string captured by the first (), $2 by the second, etc.
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #4 on: April 09, 2010, 12:02:07 PM »

DM post a munged up demo of the URL that you want to parse and let's have a go.

I was a bit ... um, relaxed last night and that's about as far as I could post on the matter Wink
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
DangerMouse
Expert
****
Offline Offline

Posts: 244



View Profile
« Reply #5 on: April 14, 2010, 02:30:26 AM »

I thought you were typing on one of those dodgy iPad keyboards Perk  Wink

The idea is to redirect "http://www.test.com/test.htm?expand=1&utm_blah=X&utm_meh=Y" to "http://www.NEW.com/NEW?utm_blah=X&utm_meh=Y".

The UTM characters are essentially the Google Analytics tracking params and they're the only part of the query string I want to pass through to the new location.

I assume I can use rewriteCond and {QUERY_STRING}, plus the technique VS suggested to capture the params, but I'm not sure if I apply the RewriteCond whether that will prevent the Redirect executing if the condition isn't matched? In this case I'm not worried if there's a match, just if there is a need to add the params on.

Any suggestions appreciated.

Ta

DM
« Last Edit: April 14, 2010, 08:11:57 AM by DangerMouse » Logged
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #6 on: April 14, 2010, 07:26:34 AM »

I assume I can use rewriteCond and {QUERY_STRING}, plus the technique VS suggested to capture the params, but I'm not sure if I apply the RewriteCond whether that will prevent the Redirect executing if the condition isn't matched? In this case I'm not worried if there's a match, just if there is to add the params on.

Yah it would prevent the RewriteRule from redirecting if there were no matches.

One workaround would be to have an additional RewriteRule with no RewriteCond.

RewriteCond blah blah
RewriteRule blah yadda
#if match do above
#if no matches...
RewriteRule blah blah

Bompa
Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!