Remove parameters within nginx rewrite - get

I'm rewriting URLs in nginx after a relaunch. In the old site I had query parameters in the URL to filter stuff e.g.
http://www.example.com/mypage.php?type=4
The new page doesn't have these kind of parameters. I want to remove them and rewrite the URLs to the main page, so that I get:
http://www.example.com/mypage/
My rewrite rule in nginx is:
location ^~ /mypage.php {
rewrite ^/mypage.php$ http://www.example.com/mypage permanent;
}
But with this rule the parameter is still appended. I thought the $ would stop nginx from processing further values... any ideas? All other questions deal with how to add parameters - I just want to remove mine :)

Had a similar problem, after a lot of searching the answer presented itself in the rewrite docs.
If you specify a ? at the end of a rewrite then Nginx will drop the original $args (arguments)
So for your example, this would do the trick:
location ^~ /mypage.php {
rewrite ^/mypage.php$ http://www.example.com/mypage? permanent;
}

To drop a parameter from a URL, in this case coupon=xxx:
if ($query_string ~ "^(.*)coupon=(.*)$") {
rewrite ^(.*)$ $uri? permanent;
}
Note that this will drop all parameters if the
statement matches. $uri is the original request without parameters.

Try setting the $args variable to empty inside the location.
set $args '';

If you want to remove a specified parameter from url,
# in location directive:
if ($request_uri ~ "([^\?]*)\?(.*)unwanted=([^&]*)&?(.*)") {
set $original_path $1;
set $args1 $2;
set $unwanted $3;
set $args2 $4;
set $args "";
rewrite ^ "${original_path}?${args1}${args2}" permanent;
}
then visit your_site.com/a=1&unwanted=2&c=3
step1. server gives an 302 response, indicating the url is match.
step2. client re-send a request with the new url ( with the parameter removed)

If we append a ? to the end of the destination it will prevent the query from being appended to the destination. :)
Example
Source
Args
Destination
Keep the arg appended
rewrite ^/mypage.php$
arg=something
https://example.com/new-page/
Do not preserve the arg
rewrite ^/mypage.php$
arg=something
https://example.com/new-page/?

Related

Nginx mapping with regex does not accept query string [duplicate]

I have this NGINX configuration:
root /var/www/web;
index index.php;
server_name domain.com;
access_log off;
error_log on;
location / {
rewrite ^/(.*)$ /index.php?tag=$1&page=1 last;
}
Now, I want to redirect url something like
"domain.com/index.php?tag=1&section=2&type=3" to "domain.com/tag/section/type"
how can I do that, where should I put the code? please help,
Thankyou
I already tried:
location / {
rewrite ^/index\.php?tag=(.*)&section=(.*)&type=(.*)$ /$1/$2/$3 permanent;
rewrite ^/(.*)$ /index.php?tag=$1&page=1 last;
}
but it didnt work..
The rewrite and location directives use a normalized URI which does not include the query string.
To test the query string, you will need to consult the $request_uri or $args variable using an if statement and/or map directive.
The advantage of using $request_uri is that it contains the original request and will help to avoid a redirection loop.
If you only have one redirection to perform, the map solution is probably overfill.
Try:
if ($request_uri ~ ^/index\.php\?tag=(.*)&section=(.*)&type=(.*)$) {
return 301 /$1/$2/$3;
}
location / {
...
}
See this caution on the use of if.

Using htaccess to redirect to certain extension

I want to know how to use .htaccess to redirect a certain path to a certain extension. To be more clear, I want to redirect something like this:
http://www.example.com/api/some/page
To this:
http://www.example.com/some/page.json
I understand that I could just do this using the router that is supplied by CakePHP, however, how would this be done with a .htaccess file?
To handle this rewrite, you may use this rule just below RewriteEngine On:
RewriteEngine On
RewriteRule ^api/(?!.+\.json$)(.+)$ $1.json [L,NC]
(?!.+\.json$) is a negative lookahead that skips matching URIs that end with .json (to avoid a rewrite loop)
Pattern ^api/(?!.+\.json$)(.+)$ matches URIs that start with /api/ and captures part after /api in $1
$1.json in target adds .json at the end of matched part
Flags: L is for Last and NC is Ignore case

Nginx config rewrite rule combination

So I have these 2 rules for url rewrites.
location ~ /details {
rewrite ^/details/(.*)/(.*)/(.*)/(.*)_(.*).html$ /site/$4.$5.html permanent;
rewrite ^/details/(.*)/(.*)/(.*)/(.*)_(.*)_(.*).html$ /site/$4.$5.$6.html permanent;
}
But for some reason the first one on its own works just fine but the second one will not pick up.
Is there a way I could combine these 2 rules into 1?
Thanks for any suggestions.
Try switching the order:
location ~ /details {
rewrite ^/details/(.*)/(.*)/(.*)/(.*)_(.*)_(.*).html$ /site/$4.$5.$6.html permanent;
rewrite ^/details/(.*)/(.*)/(.*)/(.*)_(.*).html$ /site/$4.$5.html permanent;
}
Because (.*) matches everything, it'll gobble up everything including _ characters, so your first regex matches everything the second one does and thus the second rule never gets reached.

Dynamic subdomain ditection is cakephp

I need to detect subdomain in cakephp. I assume this can be done through .htaccess rules but I'm a newbie and not much knowledge with .htaccess
You dont need to use .htaccess I found the solution using regex.
$url = "abc.yourdomain.com";
preg_match('/^(?:www\.)?(?:(.+)\.)?(.+\..+)$/i', $url, $matches);
$subdomain = empty($matches[1])? '' : $matches[1];
you will get abc in the subdomain, for www.yourdomain.com or yourdomain.com that will be empty

Redirect 301 cgi-bin/ to new URL

it's my first question here :)
I got a problem for redirecting URL:
I have old URL like www.domain.com/cgi-bin/category.cgi?type=...
And try to redirect them to www.domain.com on the htaccess
but I still have 404 error...
This is my rule :
RewriteRule ^cgi-bin/(.*)$ http://www.domain.com [R=301,L]
I verified if there are something in the conf about cgi-bin but nothing.
I did a test with "cgi-bin2" and it works...
So what can i do ?
I don't know where you problem come from but why don't you try to write a perl script which will redirect to your base domain url ?
(it can work if you have, for example, just few cgi files previously used).
In your example it seems you want to redirect "category.cgi".
so, in our case, write a "category.cgi" file in your "cgi-bin" folder and write this code inside it :
#!/usr/bin/perl
#
# fixedredir.cgi
use strict;
use warnings;
my $URL = "http://www.yourdomain.com/";
print "Status: 301 Moved\nLocation: $URL\n\n"
Hope it help !

Resources