Convert htaccess file to nginx try_file for subfolder - .htaccess

RewriteEngine On
RewriteRule ^([^\.]+)$ $1.html [NC,L]
How to convert apache .htaccess file to nginx try_file?
I have static html files in the following folder structure and accessing them with pretty URL style without .html extensions it was working with apache but when I try to convert nginx config it was throwing 404 for subfolders. I used https://winginx.com/en/htaccess site for conversion.
# nginx configuration
location / {
rewrite ^/([^\.]+)$ /$1.html break;
}
+ root-folder
+ index.html - url - www.hello.com
+ abc.html - url - www.hello.com/abc
+ .........
+ xyz.html - url - www.hello.com/xyz
+ category-folder
+ category1.html - url - www.hello.com/category-folder/category1
+ category2.html - url - www.hello.com/category-folder/category2
+ category3.html - url - www.hello.com/category-folder/category3
+ page-folder
+ page1.html - url - www.hello.com/page-folder/page1
+ page2.html - url - www.hello.com/page-folder/page2
+ page3.html - url - www.hello.com/page-folder/page3
Any help will be really apricated.

The try_files is a good directive for front-controller patterns of dynamic frameworks powered by PHP or other languages, where you have a single bootstrap file, e.g. /index.php, which "serves" the SEO friendly URLs:
/foo/ -> /index.php
/bar/ -> /index.php
/lorem/ipsum -> /index.php
If your files are static and you know the file structure in advance, try_files brings nothing other than the performance penalty of stat (file existence check) system calls.
E.g. applying try_files $uri $uri.html $uri/ =404; from a comment by Richard, would mean that for URI /category-folder/category1, NGINX first checks that actual file /category-folder/category1 exists first, then checks if /category-folder/category1.html exists, and in case it doesn't, check if /category-folder/category1/ directory exists.
It makes much sense to stick to rewrite and converting configuration, in this case, is pretty straightforward. It will result in 1:1 match on how it worked in Apache.
Using my "fairly sophisticated" Apache to NGINX config converter, which does detect when you need to use try_files and when not, you will get the desired rewrite (place this directly in server context, not in location):
rewrite ^/([^\.]+)$ /$1.html last;
To check this rewrite you can use this test.
As you see, the only trick there is that URIs in NGINX rewrites (and URIs specified in NGINX in general) have to start with slash, whereas in Apache they are omitted.

Related

conversion of htaccess to nginx is not working

I am redirecting to index page with 2 parameters which is inside child folder.
Root folder - child folder - file -> url should be like domain_name.com/child_folder/Arg1/Arg2.
I'm able to do it with htaccess. but in nginx it is not working.
this is the code in htaccess.
RewriteRule ^apply-to-speak/([a-z0-9-]+)/([a-z0-9-]+)/?$ apply-to-speak/index.php?tid=$1&pid=$2 [L,NC,QSA]
htaccess to nginx converter gave this code -
location /apply {
rewrite ^/apply-to-speak/([a-z0-9-]+)/([a-z0-9-]+)/?$ /apply-to-speak/index.php?tid=$1&pid=$2 break;
}
this is redirecting to 404 page.

Nginx rewrite subdirectory

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?

Redirecting the content of a directory, except for the directory itself and some of its subdirectories and files on Nginx

#Starkeen solved this for an Apache configuration at Redirecting the content of a directory, but not the directory itself as well as some of its subdirectories and files, but we'll soon be moving to Nginx.
His very elegant solution for Apache:
RedirectMatch ^/category/((?!index|images|menu)[^/]+)/?$ /$1
Here, everything in the /category/ directory -- except for /category/index.php, the content of /category/images/ and the content of /category/menu/ -- is redirected to the root folder.
We tried the translation from htaccess to Nginx offered at http://winginx.com/en/htaccess,
location ~ ^/category/((?!index|images|menu)[^/]+)/?$ {
rewrite ^(.*)$ /$1 redirect;
}
but that didn't work.
For some reason, when we finally give up searching for a solution and end up asking for help, we often find one soon after. Here it is -- it works perfectly:
rewrite ^/category/((?!index|images|menu)[^/]+)/(.*)$ /$1 permanent;

Nginx rewrite doesn't work when I try to match a file

I need to convert in Nginx these .htaccess rules, but when I try to target the filename and its extensions, Nginx doesn't match it. I really don't understand why of this behavior.
RewriteRule ^(.*?)(test_mod_rewrite) /$1media/plg_jchoptimize/assets/modrewrite.php?q=$2
RewriteRule ^(.*?)(gz|nz)/([^/]+)/([^/]+)/([^/]+)\.(js|css)$ /$1media/plg_jchoptimize/assets/jscss.php?f=$5&type=$6&gz=$2&d=$3&i=$4
I converted these rules in Nginx:
location /media/plg_jchoptimize/assets/ {
rewrite ^/(test_mod_rewrite) /media/plg_jchoptimize/assets/modrewrite.php?q=$1;
rewrite ^/(gz|nz)/([^/]+)/([^/]+)/([^/]+)\.(js|css)$ /$1media/plg_jchoptimize/assets/jscss.php?f=$4&type=$5&gz=$1&d=$2&i=$3;
}
The example URL is:
/media/plg_jchoptimize/assets/gz/30/0/d8604b25d503f1dcbb035ec731857648.css
What I'm wrong?

How to redirect a site to a new site

I have a site which has pages like this:
blabla.com/page/whatever
blabla.com/category/whatever
blabla.com/about
...
How can I redirect each of these to a new domain, like:
blabla.net/page/whatever
blabla.net/category/whatever
blabla.net/about
...
?
Using .htaccess
Use the Redirect directive:
Redirect / http://blabla.net/
This directive automatically preserves anything specified after the /.
It might take a bit of fiddling, but the basic idea should work here:
RewriteEngine on
RewriteRule ^(.+)$ http://blabla.net/$1 [R,NC]
You need to have mod_rewrite installed in Apache.
This says "match all URLs on this site, and redirect them to http://blabla.net/the same URL. The [R] means to actually send a redirect request to the client (so the client will make the request to the new server), rather than just serving up the page but keeping the browser URL the same. You can take the R out if you just want to serve the page but keep the old URL.
Or if you use nginx (like we at http://applehub.us, http://crazyfootball.net etc)
location ~ ^/.*_sitemap([\d]+)?.(xml|xml.gz)$ {
rewrite /(.*) /$1 break;
proxy_pass http://yourupstrem;
}

Resources