I whould like this simple rewrite rule:
http://example.com/8743b52063cd84097a65d1633f5c74f5?param1=999¶m2=2222
to be redirected to:
http://example.com/index.php?param1=999¶m2=2222&hash=8743b52063cd84097a65d1633f5c74f5
The following is my default location:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
How can I achieve this using Nginx rewrite?
Using a rewrite statement:
rewrite "^/(\w{32})$" /index.php?hash=$1 last;
Or, within a location block:
location ~ "^/(?<hash>\w{32})$" {
rewrite ^ /index.php?hash=$hash last;
}
Related
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.
I have simple nginx rewrite I can't get to work.
I have this url:
https://example.com/accessories/3427-tote-bag-grey-212345050033.html
I want to redirect to:
https://example.com/dk/accessories/3427-tote-bag-grey-212345050033.html
My nginx config:
location / {
index /index.php;
rewrite ^/dk/$1/$2.html /$1/$2.html last;
}
any idea want is wrong here?
$1 and $2 are used for captures. You have to use pattern to with groups to create captures
Try below
location / {
index /index.php;
rewrite ^/([^/]+)/([^/]+).html$ /dk/$1/$2.html last;
}
Or you can do something like below
location / {
index /index.php;
location /accessories/ {
alias <yourroot>/dk/accessories/;
}
}
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;
}
I would like to rewrite all requests that contain 'xhr.php' to /xhr.php instead of the original path.
The request:
type: "POST",
url: "/system/classes/templates/address/xhr.getAddress.php",
data: vars,
dataType: "html"
Should go to /xhr.php with the path as argument. I tried this:
rewrite ^/(.*)/xhr.(.*)\.php$ /xhr.php?parameter=$1¶meter2=$2 last;
However it does not work. Any ideas? My Config looks like this:
location / {
root /var/www/http;
try_files $uri $uri/ index.php /index.php$is_args$args #rewrite;
}
location #rewrite {
rewrite ^/(.*)/xhr.(.*)\.php$ /xhr.php?parameter=$1¶meter2=$2 last;
}
Is this basically possible with nginx? Whats wrong here?
Thanks :)
//EDIT:
I solved it, however it does not look very efficient …
location / {
root /var/www/http;
try_files $uri $uri/ index.php /index.php$is_args$args;
}
if ($request_uri ~ .*/xhr.*) {
rewrite ^/(.*)/xhr.(.*)\.php$ /xhr.php?parameter=$1¶meter2=$2 break;
}
You can remove the if and let the rewrite at the same level of location. In this way you'll save one regex for each request (the one inside the if).
Actually I think you can remove the location / too:
root /var/www/http;
rewrite ^/(.*)/xhr.(.*)\.php$ /xhr.php?parameter=$1¶meter2=$2 break;
try_files $uri $uri/ index.php /index.php$is_args$args;
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;
}
}