views:

71

answers:

3

Hi,

my URL looks like

page.html?L=0&a=1&b=2&c=6 OR page.html?a=1&b=2&L=0&c=6 OR page.html?a=1&b=2&c=6&L=0 I need to delete the L=0, where L can be either 0 or 1. But I need to keep all other params. How can this be done?

A: 

Try this rule:

RewriteCond %{QUERY_STRING} ^(([^&]*&+)*)L=[01](&+(.*))?$
RewriteRule ^page\.html$ %{REQUEST_URI}?%1%4 [L,R=301]
Gumbo
"page.html" could be anything... Can I just put (.*)? I have seen this in many examples as ultimate wildcard...
Czar
you could use .* wildcard, but I suspect that when you say it can contain 'anything' that's not really what you mean. The more specific you can be the better. [a-zA-Z0-9-]+ would be fairly common, or possibly [^./]+
Cags
Hm... I am a little bit converned to use that code as I don't understand it completly. Would you mind to explain a little bit, so I can learn as well something?
Czar
Gumbo
Hm... ok. 2 things:1. I justed check, the "L" param can be pretty much anything, from numbers to chars to any arbitrary combination.2. The Code says %1%4. Shoudln't this be %1%3 according to your explanation?
Czar
Czar
Gumbo
Ok, but what about the L Param not beeing at the end?
Czar
A: 

Ok, I played around a lot on http://gskinner.com/RegExr/.

I simply can't find a regex that works on all these cases:

domain.com/login.html?p1=1&p2=2&L=0&P3=3 domain.com/login.html?p1=1&p2=2&L=1&P3=3 domain.com/login.html?p1=1&p2=2&L=0 domain.com/login.html?&L=0 domain.com/login.html?L=0 domain.com/login.html?L=0&p1=1&p2=2&P3=3

Last best guess was (.)(L=[a-zA-Z0-9]&?)(&?)(.) >>> $1$3$4

This resulted in

domain.com/login.html?p1=1&p2=2&P3=3 domain.com/login.html?p1=1&p2=2&P3=3 domain.com/login.html?p1=1&p2=2& domain.com/login.html?& domain.com/login.html? domain.com/login.html?p1=1&p2=2&P3=3

What I try is: - Cut out the L param if it is in the middle - Cut off the L param if it is at the end of a loner param string - Cut off the L param AND "?" if it is the only param

Can you provide some more hints?

Czar
A: 

Ok, another headache and I am finally there. I just post this here in case I

  • missed something
  • maybe this can help someone else in a similar situation

.htaccess rule

# Rewrite Typo3 links with multilanguage Param "L"
RewriteCond %{QUERY_STRING} (.*)(^L=[a-zA-Z0-9]+&?|^&L=[a-zA-Z0-9]+&|&L=[a-zA-Z0-9]+)(&?.*)
RewriteRule (.*) %{REQUEST_URI}?%1%3 [L,R=301]

Explanation

The rule tries to cover all the following cases

  • domain.com/login.html?p1=1&p2=2&L=abc&P3=3
  • domain.com/login.html?p1=1&p2=2&L=def
  • domain.com/login.html?&L=eh1
  • domain.com/login.html?&L=asdfasd&p1=1&p2=2
  • domain.com/login.html?L=0
  • domain.com/login.html?L=1&p1=1&p2=2&P3=3

As far as my knowledge goes, the regex works as follow:

  1. (.*) checks if there is anything before the following L-param option
  2. The second ( )-pair group contains 3 alternatives/cases on how the L-param could be present in the query string
  3. the third (.*) simply covers everthing that might follow the L-param

Therefor the replacement only needs %{REQUEST_URI}?%1%3. This will cut out the second ( ) group with the different L-param options.

End result

The URL query string will be stripped of the L-param, but leave everything else.

Any comments? Feel free to vote if this is valuable for you, too.

Thanks to all who helped me find the correct solution!

Czar