My url looks like this:
http://site.ru/web/index.php?r=api%2Finit&user_id=1&extension_id=test
And I want rewrite it using Nginx to:
http://site.ru/api/v1/?action=init&user_id=1&bot_extension_id=test
That is, rewriting index.php?r=api only, without touching other values for r.
This is what I've tried, it isn't working though
location / {
rewrite /web/index.php?r=api%2F(.*?) /api/v1/?action=$1;
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php?$args;
}
Related
I have developers who will be working on their local machines editing multiple Wordpress sites. I'd like to set up Nginx for them one time without the need for them to edit the config file in the future. Typically when Nginx is configured to host Wordpress, a location block such as this is included:
location / {
try_files $uri $uri/ /index.php$is_args$args;
} # End location
In our situation, each WP site will be in its own subdirectory. So when a developer needs to view a site, they'll go to a URL in their browser such as:
http://localhost/site1
http://localhost/site2
http://localhost/site3
What we would like is for the location directive above to include the subdirectories. As it is now, it only includes the root (http://localhost) and not the subs. I think this requires a wildcard or regex of some kind, but I'm not sure.
In other words, I think I'm looking for a location block like:
location /all-subdirectories {
try_files $uri $uri/ /whatever-subdirectory/index.php$is_args$args;
} # End location
Does this make sense or am I on the wrong track?
You could use a regular expression location to capture the first part of the URI, for example:
location ~ ^(/[^/]+) {
try_files $uri $uri/ $1/index.php?$args;
}
Or use a named location with one or more rewrite statements, for example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^(/[^/]+) $1/index.php last;
}
I have received to migrate an existing website written in old php hosted on Apache, and I will deploy to an Nginx.
I wish to have URL like this: http://example.com/about.html
To be executed like this http://example.com/content.php?page=about
So I need to remove leading slash and remove html. The config below works if I hardcode a specific page:
location / {
try_files $uri $uri/ /content.php?page=about;
}
But of course it always serve about regardless if I access our-company.html, or our-services.html. I am not sure what I need to replace the "about" string in the config.
You should use a rewrite directive to perform the actual translation. You can invoke it from a named location specified as the last parameter on the try_files statement.
For example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/(.*)\.html$ /content.php?page=$1 last;
}
See this document for more.
On my previous server that ran apache I had some htaccess rules that helped forward a certain pattern of URL's which were giving 404's to the fixed pattern.
Long time ago my URLS for my site were http://domainname/articlename and then I changed it to be http://domainname/category/articlename
Now the problem is the older links that google has are returning 404's and I want to intercept any URL that doesn't have a category and insert a fake category and then my wordpress installation can resolve the URL.
So I'm looking for a nginx solution to this problem which I presume will be in the config file somewhere that will take this URL
http://www.criticalhit.net/prey/ (which gives a 404)
and change it to
http://www.criticalhit.net/fixed/prey/
which then resolves properly.
Use a named location to perform the rewrite, although this simple rewrite can be accomplished efficiently using a return 301.
Place a regular expression location (after the PHP location block) to bypass excluded URLs. This does not need to include the static files which are served by the try_files statement.
For example:
root /path/to/root;
index index.php;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
return 301 /category$request_uri;
}
location ~ \.php$ {
try_files $uri =404;
...
}
location ~ ^/(category|tags|feeds) {
try_files $uri $uri/ /index.php;
}
See this document for more.
I already make extensive use of rewrite in nginx to perform this sort of thing:
/photos/123344 -> /photos/photos.php?id=123344
/photos/london-2016 -> /photos/photo-shoot.php?name=london-2016
Currently I have no rule for other (non-dynamic) pages. E.g
/photos/shoot-register.php -> /photos/shoot-register.php
Which I'd like to become
/photos/shoot-register.php -> /photos/shoot-register
But without specifying an individual rewrite rule for each .php file.
It seems that try_files is the correct directive here:
location ~ ^/photos {
try_files $uri $uri.php?$args;
rewrite ^/photos/([0-9]+)(/?)$ /photos/photo.php?id=$1;
rewrite ^/photos/([^/\.]+)(/?)$ /photos/photo-shoot.php?name=$1;
}
But this doesn't work, unless I delete the two rewrite lines.
I assume that means that execution doesn't stop after try files? It finds "shoot-register.php" but then carries on executing and ends up with /photos/photo-shoot.php?name=shoot-register.php?
How can I make it stop after try_files succeeds in finding a match?
Thanks
what if you move the rewrites to separate named location, and then change your try_file directive to try the file, then the php file and then directing to the new location?
location ~ ^/photos {
try_files $uri $uri.php?$args #rewrites;
}
location #rewrites {
rewrite ^/photos/([0-9]+)(/?)$ /photos/photo.php?id=$1;
rewrite ^/photos/([^/\.]+)(/?)$ /photos/photo-shoot.php?name=$1;
}
My problem is connected with the next situation: when I`m trying to add a rule to remove slash from the url, I see the next error code "err_too_many_redirects"(if I try to check that such kind of links like site.com/images/ or other directory link return 403 code )
location / {
try_files $uri $uri/ /index.php?$query_string;
rewrite ^/(.*)/$ /$1 permanent;#remove slash
}
Could anyone help me to find soultion for this problem?
Site is working on nginx + php-fpm.
You have a situation where the following URL causes a 403 error, because the directory images actually exists:
example.com/images/
The problem is caused by the $uri/ element on the try_files directive attempting to locate the index for the directory.
By removing that element (and the rewrite directive), the requested URI should be passed to /index.php for processing as a pretty URL. Try:
location / {
try_files $uri /index.php?$query_string;
}
If you need to apply an index to some directories within your hierarchy, you can specify the rule explicitly (rather than using $uri/ and the index directive), by using (for example):
location / {
try_files $uri $uri/index.html /index.php?$query_string;
}