I am attempting to rewrite my /support url, so I can grab the "page" as an appended querystring.
The issue I am running into now, is my assets are also contained in the /support subfolder, so they too are getting re-written.
How can I change this to exclude my assets? (where assets = /support/assets/styles, /support/assets/scripts, etc...)
Here is my current location block
location /support/ {
index index.php;
rewrite ^/support/(.*) /support/index.php?_p=$1 last;
try_files $uri $uri/ /support/index.php?$args;
}
You can check for the assets with try_files before performing the rewrite by using a named location.
For example:
location /support/ {
index index.php;
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/support/(.*) /support/index.php?_p=$1 last;
}
See this document for more.
Related
This is working
https://example.com/ifsc-code/index.php?bank=axis-bank&state=west-bengal&district=kolkata&branch=bally
This is showing error404
https://example.com/ifsc-code/axis-bank/west-bengal/kolkata/bally
I am using this code in Nginx.conf file
location /ifsc-code {
try_files $uri $uri/ /index.php?$args;
rewrite ^/(.*)/(.*)/(.*)/(.*)/?$ /index.php?bank=$1&state=$2&district=$3&branch=$4;
rewrite ^/(.*)/(.*)/(.*)/?$ /index.php?bank=$1&state=$2&district=$3;
rewrite ^/(.*)/(.*)/?$ /index.php?bank=$1&state=$2;
rewrite ^/(.*)/?$ /index.php?bank=$1;
}
I changed the code and my problem has solved, hope it will help someone.
location /ifsc-code {
root /var/www/example/ifsc-code;
index index.php index.html index.htm;
try_files $uri $uri/ #ifsc-code;
}
location #ifsc-code {
rewrite ^/ifsc-code/(.*)/(.*)/(.*)/(.*)/?$ /ifsc-code/index.php?bank=$1&state=$2&district=$3&branch=$4;
rewrite ^/ifsc-code/(.*)/(.*)/(.*)/?$ /ifsc-code/index.php?bank=$1&state=$2&district=$3;
rewrite ^/ifsc-code/(.*)/(.*)/?$ /ifsc-code/index.php?bank=$1&state=$2;
rewrite ^/ifsc-code/(.*)/?$ /ifsc-code/index.php?bank=$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.
I have no problems with redirect, but when I uncomment 2 lines that do "if folder doesn't exist" check, I get 404 error on any page. And I don't understand why, it's basic functionality.
I need to redirect all requests with trailing slash to urls without it. The only exception is when a folder with that url exists.
location / {
#if (!-d $request_filename) {
rewrite ^/(.*)/$ /$1 permanent;
#}
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$request_uri;
}
You should read this caution on the use of if.
You could try an alternative approach and only rewrite after try_files has processed the URIs that it can:
location {
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/(.*)/$ /$1 permanent;
rewrite ^ /index.php?q=$request_uri last;
}
See this document for more.
I have multiple WordPress sites running in subdirectories.
Everything works great, but I'm looking to simplify my nginx configuration.
At the moment, when I add a location, I need to add an entry to my server {} configuration for the specified directory.
location / {
try_files $uri $uri/ /index.php?q=$args;
}
location /site1 {
try_files $uri $uri/ /site1/index.php?q=$args;
}
location /site2 {
try_files $uri $uri/ /site2/index.php?q=$args;
}
location /site2 {
try_files $uri $uri/ /site3/index.php?q=$args;
}
location /site4 {
try_files $uri $uri/ /site4/index.php?q=$args;
}
I tried adding a regex to match the subdirectory, but seem to have a problem with it.
location /([_-0-9a-zA-Z]/?) {
try_files $uri $uri/ /$1index.php?q=$args;
}
does not appear to do the trick. In theory that should match a subdirectory, or nothing, and be able to let me add new directories without having to touch the nginx configuration.
Any pointers would be appreciated.
#jedifans pointed out how to get the regex to work
Thanks. That did the trick on any pages, but when I go to / it just tries to download the index.php.
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_pass php70;
fastcgi_keep_conn on;
}
location ~ ^/([_\-0-9a-zA-Z]*/?) {
try_files /$1$uri /$1$uri/ /$1index.php?q=$args;
}
However when I go to domain.com/site1/ i get a download of the index.php not the homepage. What's missing?
I think you are missing the following from your new location block, the ~ operator that says to nginx that it's a regular expression. Try:
location ~ ^/([_-0-9a-zA-Z]*/?) {
A few regex tweaks too, ^ to say it must begin with / and * to match more than one of those characters within [ and ]
Edit: after your question update, try the following try_files directive:
try_files $uri $uri/ /$1/index.php?q=$args;
$uri should work without the matched string prepended and I have added a / in between the matched query and the index.php;
Also make sure to have index index.php; at server{} level.
So I've got 2 routes, and the first one doesn't stop the route matching, as the docs say it should:
location ^~ /p/ {
root /www/domain.com/;
try_files $uri $uri/ /path/index.html;
}
location ^~ /v/ {
root /www/domain.com/;
try_files $uri $uri/ /path/index.html;
}
location ^~ / {
root /www/domain.com/php_www/;
try_files $uri $uri/ /index.php;
location ~* \.(?:php|html)$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
}
auth_basic "Staging";
auth_basic_user_file /www/.htpasswd;
}
So if I have a url like this:
http://domain.com/p/1234567890
It matches the last route and not the first route. The problem surfaced because one of our guys added a page to the application:
http://domain.com/privacy
This was picked up by the FIRST route?? Which is where the problem is coming from.
The problem I'm having is with ^~. In the docs, it says that once this matches, it will stop matching, however the last route is always the one that loads.
Any ideas?
Upgraded to latest nginx, and re-ordered some of the directives and everything is working now.