Nginx rewrite rules with try_files - .htaccess

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.

Related

nginx dynamic change root of subdomain to folder on same level

I have this hierarchy on my server:
/var/www/domain.tld/web
/sub
The root in the /etc/nginx/sites-available/domain.tld is set like this:
server {
root /var/www/domain.tld/web;
index index.html index.php index.htm;
...
location / {
try_files $uri $uri/ =404;
}
How can I make the nginx to dynamically forward to specific folder so if I make folder in /sub for example /var/www/domain.tld/sub/subdomain so if I make request http://subdomain.domain.tld it will forward to this folder in folder sub?
So again something.domain.tld would be going to /var/www/domain.tld/sub/something but domain.tld would be going to /var/www/domain.tld/web
Thank you!
First off, welcome to StackOverflow! :)
There are probably a few ways of achieving this, but I would try a simple map to set a $my_subdir variable from the $host, which you can then use in your root. The $host variable is set automatically from the incoming request.
In this example, we're using a regular expression to check if the $host matches subdomain.domain.tld. If it does, capture the subdomain part - the bit in brackets - and use that to make the value "sub/subdomain" ($1 means use the first thing captured from the regular expression). If it doesn't match, it defaults to "web".
That way,
when the request comes in as something like http://domain.tld/foo.html, the response served is /var/www/domain.tld/web/foo.html
if the request comes in as something like http://bar.domain.tld/foo.html, the response served is /var/www/domain.tld/sub/bar/foo.html
map $host $my_subdir {
~^(?:www\.)?((?!www\.)[^.]+)\.domain\.tld$ sub/$1;
default web;
}
server {
root /var/www/domain.tld/$my_subdir;
index index.html index.php index.htm;
...
location / {
try_files $uri $uri/ =404;
}

Translated url path to query params for nginx

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.

Htaccess and nginx converting

I'm converting this htaccess file htaccess file
to nginx using
http://winginx.com/ru/htaccess
but don't understand where i should paste result. I have created
include file
include /etc/nginx/myfile
and pasted file there but when i'm reloading (restarting) Nginx it Fails.
Could you help me?
I suspect these apache .htaccess converters do not fully utilize the unique features of nginx. I would recommend trying something like this:
server {
listen 80 default_server;
server_name _;
root /var/www/example.com;
index index.php;
# if the file or directory doesn't exist, serve /index.php
try_files $uri $uri/ /index.php;
# if the request is exactly /sitemax.xml, serve sitemap_xml.php
location = /sitemax.xml {
try_files /modules/sitemap/sitemap_xml.php =404;
}
# hide regex location in a prefix location to avoid confusion
# introduced by multiple regex locations
location /pages-print {
location ~ ^/pages-print(\d+) {
try_files /modules/pages/print.php?page=$1 =404;
}
}
}
I have found solution. I tried paste result from the convertor to nginx.conf and it was mistake. I, instead created file in /etc/nginx/ and named it htac.rules (i think it doesn't matter of how you'll name it).
Then i opened file etc/nginx/sites-enabled/default and in location / inserted include /etc/nginx/htac.rules
so.
in htac.rules i inserted converting result without location / {...}
and, yeah. It solved my problem.
Thanks everyone for helping me.

Nginx redirect all media (.mp3 , image , pdf) to MaxCDN origin poll

I want to redirect all my media files to maxcdn origin poll, this might be duplicate question
but couldn't find the way i was looking for.
example:
If visitor request something like : http://domain.com/monday/day1.mp3 it needs to redirect to http://xxx.netdna.com/monday/day1.mp3.
Question: --- No To This :/
Will nginx allow .htaccess to do this job?
Or
Do I need to setup server config? How
Here is my config, which is every simple.
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/domain/public_html;
index index.html index.php index.htm;
# Make site accessible from http://localhost/
server_name domain.com;
return 301 http://XXX.YYYY.netdna-cdn.com$request_uri;
The page isn't redirecting properly
Please Help!
Will nginx allow .htaccess to do this job?
No. There is no .htaccess analogue for nginx.
This will redirect all request to folder /podcast/ to xxx.netdna.com.
location /podcast/ {
return 301 http://xxx.netdna.com$request_uri;
}
I have found solution for my problem after a huge dig search on google, but the answer was so simple, didn't realized till it turned on working perfectly for my problem... :)
All I need to add was, following code right after location /
Here is what i added and worked like charm.
try_files $uri $uri/ /index.php;

How to remove the index.php in the url?

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?

Resources