I will try to be brief. I have the following nginx rewrite url:
rewrite ^/(.*)?$ /index.php?completeURL=$1 last;
I want a url like:
http://mywebsite.com/http://www.otherwebsite.com/dir1/dirx/article.php&id=2&category=1
request:
http://mywebsite.com/index.php?completeURL=http://www.otherwebsite.com/dir1/dirx/article.php&id=2&category=1
Currently the nginx rule have a problem. Example: If the parameter contains a .php extension he looks for that file on my server.
Example: http://mywebsite.com/dir1/dirx/article.php
How can I solve this problem in your opinion?
UPDATE:
here the nginx configuration (and rewrite) files:
(config) https://gist.github.com/ivanionut/cc53c9de372b932c3937d9394d3b448c
(rewrite) https://gist.github.com/ivanionut/4df3ad9b858a54ae01461ab078adffb6
The simplest solution (assuming that the server does nothing else other than serve index.php) is to remove the usual location ~ \.php$ block and perform a rewrite ... break; in the same block as the fastcgi_pass. There are a number of ways of achieving this, including:
location / {
rewrite ^/(.*)?$ /index.php?completeURL=$1 break;
fastcgi_pass ...
...
}
An alternative strategy is to perform the rewrite only if a local file does not already exist, but you need to ensure that .php files are tested too. For example:
location / {
try_files $uri $uri/ #rewrite;
}
location ~ \.php$ {
try_files $uri #rewrite;
fastcgi_pass ...
...
}
location #rewrite {
rewrite ^/(.*)?$ /index.php?completeURL=$1 last;
}
Related
Am trying to redirect
domain.tld/blog/read.php?article=first-article to domain.tld/blog/first-article
What I tried and didn't work resulting in redirect to domain.tld/first-article
location "^blog/([^/]+)/?$" {
try_files /$uri /$uri/ /blog/read.php?article=$1;
}
location /blog {
rewrite ^/blog/?$ /blog/read.php?article=? last;
rewrite ^/blog/([-a-zA-Z0-9_]+)/$ /blog/read.php?article=$1? last;
}
location ~ "^/blog/([^/]+)/?$" {
try_files /$uri /$uri/ /blog/read.php?article=$1;
}
Thinking the issue comes from my other parts in the config and mainly second location from below
server {
...
...
location ~ "^/([^/]+)/?$" {
try_files $uri $uri/ /device.php?name=$1;
}
location ~ "^/([^/]+)/([^/]+)/?$" {
try_files $uri $uri/ /device.php?name=$1&crversion=$2;
}
...
Any pointers would help a lot
Cheers
So the fast answer is actually the fact that my config was fine first time, yet nginx config is read top bottom with first match being the one that is used.
So in the end the "fix" was adding the blog part upper in the site config
location ~ "^/blog/([^/]+)/?$" {
try_files /$uri /$uri/ /blog/read.php?article=$1;
}
I want nginx to rewrite the url to specific php files that can be determined by the content before the first slash
For example:
testing.com/test or test.com/test would be rewritten to test.php
testing.com/test2/variable/another-variable would be rewritten to /test2.php?q=variable/another-variable
I would then use PHP to explode the q GET parameter.
What I currently have tried is:
location / {
try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
}
This works for example 1 I have displayed above, but returns a 404 for example 2 with a more complicated URL.
You can use a named location with the try_files directive to implement one or more rewrite statements. See this document for details.
For example:
location / {
try_files $uri $uri/ $uri.html #php;
}
location #php {
rewrite ^(/[^/]+)$ $1.php last;
rewrite ^(/[^/]+)/(.*)$ $1.php?q=$2 last;
}
location ~ \.php$ {
try_files $uri =404;
...
}
The rewrite statements are evaluated in order. The second try_files statement ensures that the PHP file actually exists and is to avoid passing uncontrolled requests to PHP.
So I have this configuration at the moment in nginx:
autoindex off;
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.html index.htm index.php;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
Now, I want to get something 'special'. I want to get an url (http://project2.local/camera?camera_id=1) to be rewritten as http://project2.local/camera/1 I've tried this code;
location / {
rewrite ^/?camera\.php$ /camera/%1? redirect;
}
location /camera {
rewrite ^/camera/([^/]*)$ /camera.php?camera_id=$1 break;
}
but that downloads something empty when I navigate to that place.. What am I doing wrong here?
Try:
location /camera {
rewrite ^/camera/([^/]*)$ /camera.php?camera_id=$1 last;
}
The rewrite...break will prevent the camera.php URI being processed by the PHP location. You need to use last as per your existing configuration. See this document for details.
You also added: location / { rewrite ^/?camera\.php$ /camera/%1? redirect; }, but it seems wrong and superfluous for a number of reasons:
You cannot have two location / blocks (perhaps you meant you added the line of code to the existing location / block)
I do not recognise the %1 term (the value of the parameter would be $arg_camera_id)
It will never match anything (URIs ending with .php are processed by the PHP location)
I want to rewrite urls with nginx.
Samples:
/something.php (not regular file) -> /index.php?site=something
/somthingelse.php (regular file) -> /somethingelse.php
My current rules doesn't work:
location / {
try_files $uri $uri/ #rules;
}
location #rules {
rewrite ^/([a-z]*)\.php$ /index.php?s=$1;
}
Your /something.php url is catched by location ~ \.php$, so you need rewrite it there.
location ~ \.php$ {
try_files $uri #rules;
# usual php stuff
...;
}
location #rules {
rewrite ^/([a-z]*)\.php$ /index.php?s=$1 last;
return 404;
}
For existing file /somefile.php it will be processed by PHP as usual.
For non-existent /other.php it will be internally redirected to #rules where it will be rewritten to /index.php?s=other and gets again into location ~ \.php and finally processed by PHP.
And for non-existent /w31rd.php (where w31rd doesn't match [a-z]* regexp) you will get 404 Not Found error page.
I'm trying to work on a single page app - I need to rewrite all urls to index.html but allow existing static files (.css and .js) to be served as they normally would be in a browser.
This is the code that I'm trying to use to re-write but it serves my static files to the re-write as well
if (!-e $request_filename)
{
rewrite ^/(.*)$ /?/$1 last;
break;
}
you don't actually need a rewrite for that in nginx, just use try_files like so:
location / {
try_files $uri /index.html;
}
what this does is for all url's:
try the exact static filename match, and serve it if present
if 1 didn't serve anything, then server /index.html instead
see http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
This should work:
server {
listen 1.2.3.4:80;
server_name domain.eu;
root /usr/local/www/domain.eu/public;
try_files $uri #rewrites;
location #rewrites {
rewrite ^/favicon.ico$ /pictures/favicon.ico last;
rewrite ^ /index.html last;
}
}