How to unescape QUERY_STRING in mod_rewrite? - mod-rewrite

Hi all,
Now I want to use mod_rewrite module in apache2 to redirect url.
The rewrite rule looks like:
RewriteCond %{QUERY_STRING} ^url=(.+)$
RewriteRule ^/redir$ %1 [R=301,L]
However, when http://website.com/redir?url=http%3A%2F%2Fwww.google.com is input, the mod_rewrite module cannot unecsape the url parameter http%3A%2F%2Fwww.google.com, is there any method to resolve this problem?

RewriteMap unescape int:unescape
RewriteCond %{QUERY_STRING} ^url=(.+)$
RewriteRule ^/redir$ ${unescape:%1} [R=301,L]
Apache lets you define custom rewrite mappings from different types of external sources. For example, if you wanted to rewrite /users/<some alias> to /users/<full name>, you could have a text file that specified alias/name pairs, and a rewrite rule that translated the "alias" part of the URL using that mapping.
Mappings can come from multiple types of sources. The alias/name example is the standard plain text (txt) type.
RewriteMap also lets you map to a handful of special internal sources (int). They just pass the value to an internal Apache function and return the result. They are:
toupper: Converts the key to all upper case.
tolower: Converts the key to all lower case.
escape: Translates special characters in the key to hex-encodings.
unescape: Translates hex-encodings in the key back to special characters.
unescape is what you're looking for.
More information can be found in the mod_rewrite documentation.

Yep, there is one method: give it to a Php file then make a redirection in Php with appropriate "header".
Something like:
RewriteCond %{QUERY_STRING} ^url=(.+)$
RewriteRule ^/redir$ /myredir.php?redir=%1 [R=301,L]
And in Php, in the file myredir.php something like:
<?php
if (isset($_GET['redir'])) {
header("Location: ".urldecode($_GET['redir']));
}
exit;
?>

Related

Rewriting an URL to become a query string

I'm trying to rewrite URLs such as
/product/16/var1/value1/var2/value2...
to this
index.php?page=product&id=16&var1=value1&var2=value2...
In other words, I would like to have a "main parameter" translated to an id (and I can do this), but I would also like to have, from that point on, couples of "directories" translated recursively to key-value pairs.
Is this possible with Apache mod_rewrite?
In the absence of the [L] flag, any mod_rewrite rule will apply repeatedly to any URI which corresponds to the rule's rewrite conditions and pattern.
Knowing this, we can build a mod_rewrite rule which looks for any URIs with query strings beginning in a certain way and then repeatedly harvests the folder-names of that URI (two at a time) to build the rest of the query string.
See example below:
In the root folder of
http://example.com/
save an .htaccess file with the following mod_rewrite directives:
RewriteEngine On
RewriteRule ^(product)/([0-9]{2})/(.*) http://%{HTTP_HOST}/$3/index.php?page=$1&id=$2
RewriteCond %{QUERY_STRING} ^(page=product&id=[0-9]{2}.*)
RewriteRule ^([^/]+)/([^/]+)/(.*/)?index.php$ http://%{HTTP_HOST}/$3index.php?%1&$1=$2
Using the above:
http://example.com/product/16/var1/value1/var2/value2/
becomes
http://example.com/index.php?page=product&id=16&var1=value1&var2=value2
and
http://example.com/product/16/var1/value1/var2/value2/var3/value3/var4/value4/
becomes
http://example.com/index.php?page=product&id=16&var1=value1&var2=value2&var3=value3&var4=value4

Mod Rewrite + QSA : Same querystring param in rewrite target and in querystring - Force use of target possible?

We have the following rewrite rule:
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?rewrite=$1 [L,QSA]
We were wondering if there was a way to have the ?rewrite=$1 take precedence over one that is passed via the query string in the request uri?
Because as it stands now, due to the QSA flag (which we do need btw) if the following url is hit:
http://www.domain.com/this_rewrite_will_match_the_above_rule?rewrite=some_value
The value of $_GET['rewrite'] in PHP will be some_value, and not this_rewrite_will_match_the_above rule.
Before we go in and start modifying the rewrites and adding a RewriteCond to match the query string, etc etc... We were hoping there was a flag to set so that the target url (index.php?rewrite=$1 in this case) took precedence over the passed query string values.
Hope this makes sense.
Thanks.
Slightly hackish
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?%{QUERY_STRING}&rewrite=$1 [L]
This works because, in php, the second rewrite=... overwrite the first.
I have searched for some mechanics to override single querystring parameters in an Apache rewrite multiple times and quite intensely, but it looks like there is no option to do that, even with the latest Apache version (2.4.3 at the time of this writing).
But there is an alternative that makes use of the fact that the PHP querystring parser only returns the last of multiple idententical querystring parameters.
Example:
http://www.domain.com/index.php?id=123&id=456
This will return the following single (!) entry in $_GET:
array(1) {
["id"]=>
string(3) "456"
}
So you can solve your problem by simply appending any override parameters to the end of your existing querystring (without removing them within the querystring). The last occurrence of a repeated parameter is the one that makes it into the $_GET array.
Unfortunately the QSA switch is not suitable for this technique, as it always appends the original parameters to the end of the new querystring. There is no switch that would preprend the old parameters. So you have to take a little detour with a RewriteCond to catch and prepend the original querystring yourself instead of using QSA:
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?%1&rewrite=$1 [L]
The only function of the RewriteCond is to capture the querystring in %1. The regexp (.*) of the condition is always matched, so the following RewriteRule is always executed.
With this technique your above testcase will rewrite to...
http://www.domain.com/index.php?rewrite=some_value&rewrite=this_rewrite_will_match_the_above_rule
...which will be interpreted by the PHP querystring parser to
$_GET["id"] => "this_rewrite_will_match_the_above_rule"
...which is what you want.
Please be aware that this will work only if you take your querystring values from PHP's $_GET array. It will not necessarily work if you parse the content of $_SERVER["QUERY_STRING"] yourself or if you use any other programming language.
I have opted to create an answer of my own, because its slightly cleaner then the examples provided by Jpsy and Gerben.
Credit where credit is due, their suggestions are what got me here, I only expanded on them:
So, our final solution includes 2 rules.
# check if querystring is not empty (this is the addition vs other answers)
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?%{QUERY_STRING}&rewrite=$1 [L]
if the above query string fails (mainly, the querystring is empty) this rule will apply.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?rewrite=$1 [L]
The reason I opted to go for this dual rule setup is to avoid having the php server variables polluted with "?&rewrite=...." if the query string is empty.
Thanks to jpsy and gerben for the help.
I used the following instructions:
RewriteRule ([^\?]*)\?(.*) $1?$2 [N] (1)
RewriteCond %{QUERY_STRING} (.*?)&?(rewrite=[^&]+)&?(.*)
RewriteRule ^(.*)$ $1?%1&%3 [N] (2)
RewriteRule ^(.*)$ $1?domain=newValue [L,QSA] (3)
The trick is done by the [N] flag in the first two rules, which causes to rewrite engine to re-process the output.
Rule (1) simple rewrites the url as it is. I needed it because I'm using mod_rewrite togheter with mod_proxy_ajp and in the first iteration the query string is not splitted from the url. After the execution of the first line, the url is unchanged but the engine will split the path from the query string.
Rule (2) iterates and removes all occurrences of the parameter "rewrite" from the query string.
Rule (3) sets the new value for the parameter and appends whichever query string survives from the replacement done by rule (2).

Change uri parameters passing

Link example jumppage support:
http://server-address/index.php?param1={value1},{value2},{value3}
-Above is comma separated
Link example our website support:
http://server-address/index.php?param1={value1}&param2={value2}&param3={value3}
How can i reformat the initial paramaters are passed according to our standard?
Thanx!
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^param1=(.*?),(.*?),(.*)$
RewriteRule ^index\.php$ index.php?param1=%1&param2=%2&param3=%3 [L]
Note this does an internal redirect rather than a 301 or 302. Also not that is the comma separated list contain a variable number of items or there are other parameters, this can also be handled but the rules get a lot more complicated.

Remove All Query Strings from URL

I would like to rewrite my url's, so that whenever there's a ?, it and everything after is removed.
I have various strings, such as:
....html?frame=...
....html?sport=...
....html?type=...
So to make it easy, if there's a ? anywhere in the url, it should be truncated.
(By the way, I am already rewriting .php as .html)
Thanks a lot.
As pointed out in the comments:
RewriteCond %{QUERY_STRING} !=""
RewriteRule ^(.*)$ /$1?
Works, but OP says it reverts the php -> html extension conversion back.
Try it adding the php/html rewrite before:
RewriteRule ^(.*)\.php$ /$1.html
RewriteCond %{QUERY_STRING} !=""
RewriteRule ^(.*)$ /$1? [L,R=302]
I just tried it, and I'm having some issues with RewriteBase, which you might need to setup for this to work. Otherwise, looks fine.
From mod_rewrite documentation (emphasis mine):
Modifying the Query String
By default, the query string is passed through unchanged. 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 new and old query strings, use the [QSA] flag.

Mod_rewrite help

I'm trying to remove query strings from my calendar, but my mod_rewrite is not appending the query string.
The website is http://cacrochester.com/Calendar
and if you click the link to go to a different month, the query string is usually http://cacrochester.com/Calendar?currentmonth=2010-11
With my rule below, it just doesn't append the query string so when you click the next month link, it just stays on the month October. What's wrong with my rule?
Here is my rule
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^.*$ http://cacrochester.com/Calendar? [NC,R=301,L]
EDIT:
What i want is to take a url like http://cacrochester.com/Calendar?currentmonth=2010-11 and turn it into something like http://cacrochester.com/Calendar/2010-11
You probably need your app to output relative urls like "/Calendar/2010-11". That's a simple code change.
Then in Apache you'd want to rewrite those urls, using:
RewriteRule ^/Calendar/([0-9]+-[0-9]{2})$ /Calendar.php?currentmonth=$1 [NC,QSA,L]
(You don't want a RewriteCond for this rule.)
Forcing a redirect with R=301 will only expose the internal url scheme. I don't think that's what you want.
To maintain query strings when rewriting, use the QSA (query string append) flag.
[NC,R=301,QSA,L]

Resources