My Rewrite rule is not performing multiple rewritemap lookups and I am confused why. I am trying to redirect my users to a new URL search structure. The rewrite rule does not work when there are multiple search parameters and I am unsure why.
// Desired mapping examples:
http://www.host.com/search/small => http://www.host.com/search?q=tall
http://www.host.com/search/medium/brown => http://www.host.com/search?q=grande,chocolate
// Rule
RewriteMap searchMap txt:/opt/etc/apache/conf/searchMap.txt
// 1 Search Parameter
RewriteCond ${searchMap:$1|$1} ([^/]*)
RewriteRule "/search/(([^/]*))$" "http://%{HTTP_HOST}/search?q=%1" [NC,R,L]
// 2 Search Parameter
RewriteCond ${searchMap:$1|$1} ([^/]*)/([^/]*)
RewriteRule "/search/(([^/]*)/([^/]*))$" "http://%{HTTP_HOST}/search?q=%1,%2" [NC,R,L]
// 3 Search Parameter
RewriteCond ${searchMap:$1|$1} ([^/]*)/([^/]*)/([^/]*)
RewriteRule "/search/(([^/]*)/([^/]*)/([^/]*))$" "http://%{HTTP_HOST}/search?q=%1,%2,%3" [NC,R,L]
// searchMap.txt
small tall
medium grande
low-fat healthy
low-calorie healthy
brown chocolate
pink strawberry
Output:
http://www.host.com/search/small => http://www.host.com/search?q=tall
http://www.host.com/search/small/brown => http://www.host.com/search?q=small,brown
My first output is being mapped correctly, but my second one is not. Apache has not performed any mapping. Any reason why this is happening?
Solution is to put the mapping lookup within the Rewrite rule:
RewriteMap searchMap txt:/opt/etc/apache/conf/searchMap.txt
RewriteRule "/search/(([^/]*))$" "http://%{HTTP_HOST}/search?q=${searchMap:$1|$1}" [NC,R,L]
RewriteRule "/search/(([^/]*)/([^/]*))$" "http://%{HTTP_HOST}/search?q=${searchMap:$1|$1},${searchMap:$2|$2}" [NC,R,L]
Related
I'm trying to make a function that will get a string between two arguments (rewrite and redirect). I can't wrap my head around it.
I have a string that looks like this:
add_header X-Robots-Tag "noindex, follow" always; rewrite ^/en/about/Administration/index.aspx /en/about/more-about/administration redirect; rewrite ^/en/about/Administration/supervisory-board/index.aspx /nl/over/meer-over/administration redirect; rewrite ^/en/about/Departments-sections-and-fields/index.aspx /en/about/more-about/department-divisions-and-fields redirect; rewrite ^/en/about/For-companies/index.aspx /en/about/more-about/for-companies redirect; rewrite ^/en/about/contact-information/index.aspx /en/about/more-about/contact-information redirect; rewrite ^/en/about/index.aspx /nl/over redirect;
And I want the following output:
/en/about/Administration/index.aspx /en/about/more-about/administration
/en/about/Administration/supervisory-board/index.aspx /nl/over/meer-over/administration
/en/about/Departments-sections-and-fields/index.aspx /en/about/more-about/department-divisions-and-fields
/en/about/For-companies/index.aspx /en/about/more-about/for-companies
/en/about/contact-information/index.aspx /en/about/more-about/contact-information
/en/about/index.aspx /nl/over
What is the proper regex or method to get all strings between the two arguments?
Try:
\brewrite \^(.*?)\s+redirect;
See an online demo
\brewrite \^ - literally 'rewrite' between a word-boundary on the left and a space followed by literal '^' on the right;
(.*?) - Match (lazy) 0+ characters;
\s+redirect; - Literally 'redirect' between 1+ whitespace characters on the left and a semi-colon on the right.
See an online GO demo which will print:
/en/about/Administration/index.aspx /en/about/more-about/administration
/en/about/Administration/supervisory-board/index.aspx /nl/over/meer-over/administration
/en/about/Departments-sections-and-fields/index.aspx /en/about/more-about/department-divisions-and-fields
/en/about/For-companies/index.aspx /en/about/more-about/for-companies
/en/about/contact-information/index.aspx /en/about/more-about/contact-information
/en/about/index.aspx /nl/over
Following url is working fine:
http://www.domain.com/address
but when I pass any querystring like:
http://www.domain.com/address?back=order-opc.php?step=1
It shows 404 page
my rewrite:
"^/address" => "/address.php",
i have tried so many different rewrite nothing seems to work...
how should i rewrite ?
You should take in count the rest of the query string in your rule.
"^/address(\?.*)?" => "/address.php$1",
Your query string should be:
http://www.domain.com/address/?back=order-opc.php?step=1
Note the slash / as /address means /address/index.php or /address/index.html ( or whatever is your default document )
I want to rewrite the following URL:
index.php?SOMETHING=VALUE
As
/SOMETHING/VALUE
I'm inexperienced with nginx rewrites so any help would be appreciated.
Thanks
I've come up with a solution to your problem :
location /index.php {
if ( $args ~ "(?<PATH1>.*)=(?<PATH2>.*)" ) {
rewrite ^ /${PATH1}/${PATH2}? last;
}
}
Explanations:
if ( $args ~ "(?<PATH1>.*)=(?<PATH2>.*)" ) : captures the two relevant sections from the URL parameter, storing the values in variables PATH1 and PATH2
rewrite ^ means "rewrite the entire URI"
/${PATH1}/${PATH2} is constructing the new URI
the trailing ? informs nginx that you don't want to append the original URL parameters
last tells nginx to continue to follow rules after the rewrite
I want to redirect urls like http://mysite.com/?action=add&goback=1 to http://myiste.com/add/?goback=1.
I came to:
location / {
if ($request_uri ~* /\?action.*) {
add_header Cache-Control private;
rewrite ^/\?action=([^&]*)&?(.*)$ /$1/?$2 permanent;
}
}
but it does not work and I don't see why.
P.S. I needed add_header just to indicate if ($request_uri ~* /\?action.*) { took place.
I've tried several other solutions like
rewrite ^/\?action=(\w+).*$ /$1/?$query_string permanent;
no success.
UPDATE:
The actual goal is:
if URL looks like this
http://mysite.com/add/?param1=param1&...
/(add)/ should become an action param:
location ~ /[\-\w]+/ {
rewrite ^/([^/]*).*$ /?action=$1 last;
...
}
this seems to be working properly and I guess I don't need extra-help with this one
if URL looks like this:
http://mysite.com/?action=add¶m1=param1&...
nginx should redirect to:
http://mysite.com/add/?param1=param1&...
When I try to do this part I'm getting results I don't need - infinite redirect loops, URLs like http://mysite.com/add/?action=add¶m1=param1&..., etc.
Is there the solution addressing my goal?
This is because rewrite only matches URI path, without query sting aka arguments, much like location. Try something like this instead:
location = / {
if ($arg_action) {
rewrite ^ /$arg_action/ permanent;
}
}
The location = / check will make sure the rule is only applied to requests to /, if ($arg_action) checks if there is action=... argument, and rewrite ^ /$arg_action/ permanent; will actually do a redirect to rewritten URI. Query string will be preserved (as by default) from original request.
See http://nginx.org/r/rewrite for docs.
I am using the Facebook PHP SDK in order to allow users to log in to my site using Facebook.
In my test below (assume the URL is http://mysite.com/test/fbtest.php),
<?php
require_once("facebook.php");
$fb = new Facebook(array('appId' => 'APP_ID', 'secret' => 'APP_SECRET'));
$fbuser = false;
$fbuserid = $fb->getUser();
$fblogin = $fb->getLoginUrl(array(
'redirect_uri' => "http://{$_SERVER['HTTP_HOST']}/test/fbtest.php"));
if($fbuserid)
{
try
{
$fbuser = $fb->api("/me");
print_r($fbuser);
echo "<br/>\n";
}
catch (FacebookApiException $e)
{
$fbuser = false;
$fbuserid = 0;
}
}
if(!$fbuser)
echo "FB Login\n";
?>
This seems to work as expected. However, when I add the following rewrite rule,
RewriteRule ^/FBtest/(.*)$ http://%{HTTP_HOST}/test/fbtest.php$1
Then change my login redirect to the following,
$fblogin = $fb->getLoginUrl(array(
'redirect_uri' => "http://{$_SERVER['HTTP_HOST']}/FBtest/"));
Then $fb->getUser() always returns 0. I feel that I am missing something important.
In the server side flow, your redirect_uri get’s called with the necessary values as GET parameters in the query string.
'redirect_uri' => "http://{$_SERVER['HTTP_HOST']}/FBtest/"
So with this redirect_uri, something like
http://example.com/FBtest/?state=foo&code=bar
will be called on your server.
RewriteRule ^/FBtest/(.*)$ http://%{HTTP_HOST}/test/fbtest.php$1
RewriteRules don’t examine the query string, they only look at the path component of the URL. In your case, that’s /FBtest/, nothing behind it – so the internal redirect goes to /test/fbtest.php, and the query string parameters get lost, because you didn’t say you wanted to pass them on.
Add the flag [QSA] – for “query string append” – to your RewriteRule (and remove the unnecessary (.*)) – then things should work as expected, because your fbtest.php will get the query string parameters needed for the auth process.
Finally figured this problem out. Here is the RewriteRule I need:
RewriteRule ^/FBtest/$ http://%{HTTP_HOST}/test/fbtest.php [QSA,NE]
It turns out I needed to add both the QSA and NE flags to this rule.
Much like in CBroe's answer, the QSA flag was needed for the state/code parameters added to redirect_uri (using (.*) within the RewriteRule instead doesn't catch these additional parameters).
I also needed to add the NE flag because the state/code that the Facebook authentication was adding to the redirect_uri was being escaped.
You will not be able to use any other domain other than the domain you set in your applications settings.
"be sure you are using the root domain, and not sample.com/test/ as your url settings."