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§ion=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=(.*)§ion=(.*)&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=(.*)§ion=(.*)&type=(.*)$) {
return 301 /$1/$2/$3;
}
location / {
...
}
See this caution on the use of if.
Related
I plan to convert this address:
www.somewebsite.com/news/22915
To this address:
www.somewebsite.com/news/22915.html
And this is how I do it in my nginx configuration:
rewrite ^/news/(\d+)$ /news/$1\.html redirect;
But somehow, nginx will rewrite my address to:
www.somewebsite.com/news/22915/.html
Had no idea why / will go into the variable automatically, since (\d+) should only be matching pure numbers, need help.
Use rewrite ^/news/(\d+)$ /news/$1.html redirect;
The second part of the rewrite syntax is not a regex. So you should not try to escape the dot character.
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.
I am using nginx. I want to rewrite urls. My code:
rewrite ^/([^/]*)_([a-zA-Z0-9]{9}).html$ /watch.php?vid=$2 last;
Example of url:
http://104.238.130.170/hudson-against-the-grain-video_14a4e06f8.html
But when I save file then restarts nginx server i got error:
[emerg] directive "rewrite" is not terminated by ";" in /etc/nginx/conf.d/default.conf:46
QUESTION: What is wrong on my rewrite rule?
rewrite ^/([^/]*)_([a-zA-Z0-9]{9}).html$ /watch.php?vid=$2 last;
The curly brackets {9}are most likely giving a problem in your regex. Surround the rule with quotes like below and try it.
rewrite "^/([^/]*)_([a-zA-Z0-9]{9}).html$" /watch.php?vid=$2 last;
Note: for curly braces( { and } ), as they are used both in regexes
and for block control, to avoid conflicts, regexes with curly braces
are to be enclosed with double quotes (or single quotes).
More info here
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
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/?