Url https://example.com/profile/chico
must be understood by nginx as:
https://example.com/profile/?username=chico (the url that currently works).
https://example.com/profile/ currently works as well, as it currently resolves to index.php, without the username param.
I currently have
location /profile/ {
index /profile/index.php;
}
There are so many different things about this on the web and I didn't find specifically what I need.
Also tried
location /profile/ {
rewrite ^/profile/(.*)$ /profile/?username=$1;
}
Try:
location /profile/ {
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^/profile/(.+)$ /profile/index.php?username=$1 last;
rewrite ^ /profile/index.php last;
}
If the first rewrite matches, the second rewrite is ignored. After the URI is rewritten with a .php extension, the processing moves to a different location block.
See this document for more.
EDIT: Added try_files block to handle static content.
Related
I'm trying to do a rewrite for nginx. It's working but for sub directories it is taking the root's index.php instead of the index.php in the individual subdir. This is how my directory structure looks like:
/index.php
/p/index.php
/c/index.php
and this is my rewrite:
if (!-e $request_filename){
rewrite ^/([^./]+)/?$ /index.php?act=$1 break; #need break?
rewrite ^/([^./]+)/(.+)/?$ /index.php?act=$1&upm=$2 break;
rewrite ^/([^./]+)/([^./]+)/?$ /$1/index.php?act=$2;
}
I tried adding an entry for each subdirectory but it doesn't work either:
location /p/ {
root /home/user/public_html/p/;
rewrite ^/([^./]+)/(.+)/?$ /index.php?act=$1&upm=$2 break;
}
Any ideas?
The rewrite ^/([^./]+)/([^./]+)/?$ /$1/index.php?act=$2; will never be executed as the previous rewrite will always match. You might try swapping rewrites #2 and #3.
The root /home/user/public_html/p/; is probably wrong as /p/ will appear twice as it is in the $document_root and the $uri.
The rewrite in your location /p/ should rewrite to /p/index.php and not /index.php?
Nginx rewrite from .php to .php under same location
I am trying to move a site with a large number (93) of .htaccess files from Apache to Nginx.
There are over 800 rewrite rules to convert. To optimize performance, I have decided to
place the rules under separate nested location blocks (61 blocks in total).
But I am having problem with rewriting some of the rules.
Requirements:
/test/abc/pics/ should redirect to /test/abc/wallpapers/
/test/abc/pics/800/a.jpg should be served directly, if the file is present. If it does not exist,
then the request should be rewritten to /test/abc/pics/pic.php, which will
create the file on first request. Subsequent requests will be served directly.
/test/abc should redirect to /test/abc.html
/test/abc.html should be rewritten to /test/index.php
/test/abc/def/year-2014.php should be rewritten to /test/abc/def/index.php
All static files (.gif, .jpg, .png, .mpg....) should be served directly with a very long expiry time.
All files ending in .html should be served directly. If the file does not exist the request should be rewritten to /fallback.php
All files ending in .php or / should be executed by PHP-FPM. If the file does not exist it should be rewritten to /fallback.php
Config:
index index.php;
#
# Location Specific Rules
#
location /test/abc/ {
location /test/abc/pics/ {
# For ensuring 'serve directly, if file exists' condition of Rule 2
try_files $uri #rewrite_pics;
}
location /test/abc/def/ {
try_files $uri $uri/index.php #rewrite_def;
}
}
location /test/ {
rewrite "^/test/([a-z-]+)$" /test/$1.html permanent; # Rule 3
rewrite "^/test/([a-z-]+).html$" /test/index.php?symbol=$1 last; # Rule 4
}
location #rewrite_pics {
rewrite "^/test/abc/pics/$" /test/abc/wallpapers/ permanent; # Rule 1
rewrite "^/test/abc/pics/([0-9]+)/(.*)$" /test/abc/pics/pic.php?width=$1&height=$1&pic=$2 last; # Rule 2
}
location #rewrite_def {
rewrite "(?i)^/test/abc/def/year-([a-z]+).php$" /test/abc/def/index.php?year=$1 last;
}
#
# Fallback Rules
#
location ~* \.(gif|jpg|jpeg|png|ico|3gp|flv|mpg|mpeg|mp4|mp3|swf|txt|xml|pdf|zip)$ {
expires 1M; # Rule 5
try_files $uri /404.php;
}
location ~ (\.php|\.html|/)$ {
try_files $uri "${uri}index.php" /fallback.php =404;
include php-fpm.conf;
}
php-fpm.conf
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
Problem:
Since the fallback rules are regular expressions, they are selected instead of the location specific rules
If I use ~ prefix for all location specific rules, then the fallback rules are never executed.
Request for /test/abc/pics/800/a.jpg will be rewritten to /test/abc/pics/pic.php which will again match the
same location config. So the file is not executed and is downloaded as is.
So, I added 'include php-fpm.conf;' to all location blocks (except named locations). But this causes all
requests under /test/abc/pics/ (including existing .jpg files) to pass to the PHP-FPM. Which leads to an access denied error,
since only .php extension is allowed by security.limit_extensions.
What am I doing wrong here? Is there a simple way to have all existing files served based on their extension
and only non existent files match the location blocks / rewrite rules?
I need help to converting this .htaccess to Nginx. Here is the .htaccess code:
RewriteRule wp-content/thesis/skins/(.*)/css.css wp-admin/admin-post.php?action=thesis_do_css
I already convert those rules to online nginx converter. So far I get this code:
rewrite /wp-content/thesis/skins/(.*)/css.css /wp-admin/admin-post.php?action=thesis_do_css;
I put those code above under try_files rules. Here is the complete code:
location / {
try_files $uri $uri/ /index.php?q=$uri$is_args&$args;
rewrite /wp-content/thesis/skins/(.*)/css.css /wp-admin/admin-post.php?action=thesis_do_css;
}
So, it doesn't work. Please help me understanding about nginx rules. Thanks.
Beny
try_files is final. You cannot put anything after it and expect it to be evaluated. Think of try files as an if-then-else statement, where the last part is always the else. Since there are probably a lot more rules involved, you are best off consulting the WordPress codex.
If you need just to rewrite that one css.css file to wp-admin/admin-post.php?action=thesis_do_css then try this:
if ($request_filename ~ wp-content/thesis/skins/classic-r/css.css ) {
rewrite ^ http://mydomain.com/wp-admin/admin-post.php?action=thesis_do_css? permanent;
}
That should be within your server{ block. Make sure to change mydomain.com to your actual domain, of course.
I hope that helps.
my host server is nginx.
in the root directory i have installed magento, then i created a file named blog, then installed wordpress into it.
now, the directory is:
app
includes
....
blog/wp-admin
when i set http://www.example.com/blog/index.php/%postname%.html in Permalink Settings. access all the post, it's ok. now i want to remove the index.php. namely change it into http://www.example.com/blog/%postname%.html.when i delete the index.php. all the post are not unavailable,
EDIT:
i put the code in the nginx.conf.
location /blog/ {
index index.php;
try_files $uri $uri/ /blog/index.php?$uri&$args;
rewrite ^ /index.php? last;
}
but it doesn't work. is there something i lost?
I am trying to redirect from:
http://domain.com/public/photos/large/test.jpg
to:
http://domain.com/images/public/photos/large/test.jpg
on a NGINX environment.
My vhost file is configured like the next lines.
My problem is that a syntax like this is not working:
server {
........
rewrite ^(public/photos/(.*)) http://domain.com/images/$1 last;
location / {
location ~.*\.(3gp|jpg|jpeg|gif)$ {
expires 7d;
try_files $uri #backend;
}
}
..........
}
The rewriterule syntax was tested on a .htaccess environment and it worked for more than 2 years now. On nginx it doesn't anymore.
Any ideas?
10x
Try this:
rewrite ^/public/photos/(.*)$ http://domain.com/images/public/photos/$1 last;