htaccess question: Remove 1(!) param from URL query - apache

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?

Try this rule:
RewriteCond %{QUERY_STRING} ^(([^&]*&+)*)L=[01](&+(.*))?$
RewriteRule ^page\.html$ %{REQUEST_URI}?%1%4 [L,R=301]

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?

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:
(.*) checks if there is anything before the following L-param option
The second ( )-pair group contains 3 alternatives/cases on how the L-param could be present in the query string
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!

Related

Rewriting 2nd parameter in query string

Like so many I am struggling with what ought to be a simple rewrite
The initial form is something like:
http://cassie-family.co.uk/individual.php?pid=I807&ged=Cassy%20Family%20History.ged
The last parameter only needs rewriting, as in:
http://cassie-family.co.uk/individual.php?pid=I807&ged=Cassie%20-%20Cassy%20Family%20History
I have tried to trap the first parameter using:
RewriteCond %{QUERY_STRING} ^pid=([^&]+) [NC]
The nearest (incorrect) RewriteRule is:
RewriteRule ^(.*)$ individual.php&pid=%1&ged=Cassie%20-%20Cassy%20Family%20History [L, NC, R=301]
The moment I try to replace the '&' after individual.php with the correct '?' the URL is written as:
http://cassie-family.co.uk/individual.php
So.. the parameter is being trapped correctly but the rewrite rule is clearly mangled. The error lies in here:
^(.*)$ individual.php&
What construct should I be using to replace only the 2nd (passed-in) parameter value (which is always the same fixed string) with a different 2nd value (which is always the same but slightly different fixed string)? Adding a B flag seems to make no difference.
All suggestions welcome,
Thanks,
Ric

Mod_rewrite. Redirect url with Special Characters (question marks)

I have a website with joomla and I need to redirect (301) some links
They are in this form (index.php?Itemid= identify them - all links that doesn't have this part shouldn't be redirected)
/index.php?Itemid=544&catid=331:savona&id=82356:smembramento-dei-cantieri-baglietto-di-varazze-lopposizione-delle-maestranze&option=com_content&view=article
This should work
RewriteRule ^index.php?Itemid(.*)$ http://www.ligurianotizie.it/archive/index.php?Itemid$1 [L,R=301]
But the first ? (question mark) seems to cause problems.
In fact, if we suppose that the links are without the question mark
/index.phpItemid=544&catid=331:savona&id=82356:smembramento-dei-cantieri-baglietto-di-varazze-lopposizione-delle-maestranze&option=com_content&view=article
I would use
RewriteRule ^index.phpItemid(.*)$ http://www.ligurianotizie.it/archive/index.php?Itemid$1 [L,R=301]
and everything is perfect. But unfortunately real links has that question mark, and I have to find a solution.
What I have to do with that question mark?
Is the ? character escaped? try to add the NE (noescape) flag like this:
RewriteRule ^index.php?Itemid(.*)$ http://www.ligurianotizie.it/archive/index.php?Itemid$1 [L,R=301,NE]
The part behind the question mark is the query string. You can use RewriteCondto determine if it is not empty, and based on that make the decision to redirect.
Note: Query String
The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.
Source: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
This should help you:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} Itemid
RewriteRule ^index.php(.*)$ http://www.ligurianotizie.it/archive/index.php$1 [L,R=301]
Every link containing "Itemid" will be redirected, the others not.

How do I Mod_Rewrite Entire URL Path?

I have been using this rule:
RewriteRule ^([^\.]+)/?$ index.php?page=$1 [L]
Which will turn
example.com/page/hello
...into
example.com/index.php?page/hello
However, if I have a file extension on the end of that, say,
example.com/page/hello/doc.html
...it doesn't work. How can I make that URL become:
example.com/index.php?page/hello/doc.html
...and
example.com/page/
...become:
example.com/index.php?page/
Thank you very much if you are able to help with this. I've tried fiddling with the regular expression, but I just can't make it work.
Thanks.
This section of your regex will match one or more characters that are anything except a full stop:
[^\.]+
If you want to match one or more of any character, then just write:
.+
instead

question regarding specific mod_rewrite syntax

I know there are other questions that are similar to this.. but I'm really struggling with mod_rewrite syntax so I could use some help.
Basically, what I am trying to do is have the following redirect occur:
domain.com/1/ redirect to domain.com/?id=$1 (also should work for www.domain.com)
What I have so far (that isn't working):
RewriteEngine On
ReRewriteRule ^/([0-9])$ /?id=$1
A few issues.
First is terminology: if you want when a user types domain.com/1/ that the request is served by index.php?id=1, then you are rewriting /1/ to index.php?id=1, not the other way around as you said.
Second, simple typo: RewriteRule, not ReRewriteRule.
Second, [0-9] is the right way to match a number, but it'll only match a single digit. If you want to handle /13 then you should match one or more instances [0-9] by writing [0-9]+.
Third, the target of your rule should be the file you want to serve. / is not a file or an absolute URL, write out the index.php if that's what you mean.
Third, you say you want to handle /1/, but your rule says that the matched request must end in a number, not a slash. If you want to accept the slash whether it's there or not, put that in the rule.
RewriteRule ^/?([0-9]+)/?$ index.php?id=$1 [L]
Does that work?
You've three issues:
RewriteRule is misspelt as point out by Michael, you need to worry about the trailing slash, and you need to stop processing rules when you've found the match:
RewriteRule ^/(\d+)/?$ /?id=$1 [L]
You have misspelled RewriteRule. Otherwise, I think your syntax looks correct.
RewriteEngine On
ReRewriteRule ^/([A-Za-z0-9]+)$ /?id=$1
--^^^---------
Actually, you should probably remove the /:
RewriteEngine On
RewriteRule ^([A-Za-z0-9$_.+!*'(),-]+)$ /?id=$1
------^^^---------
EDIT Added the +. Look at all the answers here. You need a composite of them., including the + and the [L] in addition to what I have here.
EDIT2 Also edited to include alpha characters in the id.
EDIT3 Added special characters to regex. These should be valid in a URL, but it's unusual to find them there.

Mod Rewrite problem

I have a problem that I cannot wrap my head around.
I'm using Apache and PHP
I need to get:
http://localhost.com/cat_ap.php?nid=5964
from
http://localhost.com/cat_ap~nid~5964.htm
How do I go about changing that around? I have done more simple mod rewrites but this is slightly more complicated. Can anyone give me a leg up or point me in the right direction
RewriteRule ^/cat_ap~nid~(.*)\.htm$ /cat_ap?nid=$1 [R]
The [R] at the end is optional. If you omit it, Apache won't redirect your users (it will still serve the correct page).
If the nid part is also a variable, you can try this:
RewriteRule ^/cat_ap~([^~]+)~(.*)\.htm$ /cat_ap?$1=$2 [R]
EDIT: As Ben Blank said in his comment, you might want to restrict the set of valid URLs. For example, you might want to make sure a nid exists, and that it's numerical:
RewriteRule ^/cat_ap~nid~([0-9]+)\.htm$ /cat_ap?nid=$1
or if the nid part is a variable, that it only consists of alphabetical characters:
RewriteRule ^/cat_ap~([A-Za-z]+)~([0-9]+)\.htm$ /cat_ap?$1=$2
Assuming the variable parts here are the "nid" and the 5964, you can do:
RewriteRule ^/cat_ap~(.+)~(.+).htm$ ^/cat_ap?$1=$2
The first "(.+)" matches "nid" and the second matches "5964".
If you want everything arbitrary:
RewriteRule ^/(\w+)~(\w+)~(\w+)\.htm$ $1?$2=$3 [L]
Where \w is equal to [A-Za-z0-9_]. And if you want to use this rule in a .htaccess file, remove the leading / from the pattern.