nginx rewirte in two different folder (folder + sub folder different rewirte) - .htaccess

I have different problem with nginx rewrite.
In apache I use to htaccess file in root and sub directory.
Use in root folder:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?req=$1 [QSA,L]
</IfModule>
and in subfolder with cp:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ router.php?get=$1 [QSA,L]
</IfModule>
But when i try to configure nginx by this:
but what i try:
server {
listen xx.xx.xx.xx:80;
server_name mydomain.net www.mydomain.net;
root /home/admin/web/mydomain.net/public_html;
index index.php index.html index.htm;
access_log /var/log/nginx/domains/mydomain.net.log combined;
access_log /var/log/nginx/domains/mydomain.net.bytes bytes;
error_log /var/log/nginx/domains/mydomain.net.error.log error;
#first block
location / {
if (!-e $request_filename){
rewrite ^(.+)$ /index.php?req=$1 last;
}
}
#second block
location /cp {
if (!-e $request_filename){
rewrite ^(.+)$ /router.php?get=$1 last;
}
}
}
But when i Try download force php.
This problem not look like other problems i search many about this and I cant find the answer
when I remove second block rewrite work on root.

For location issue you should check this answer. For processing php you should configure php-fpm and add to nginx configuration php-processing location:
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/nginx/$fastcgi_script_name;
fastcgi_index index.php;
}

Related

Nginx server config URL rewrite to /backend and /frontend directories

I am trying to convert .htaccess file to a nginx server config.
There is default client side site which is served from - /frontend/public - this works fine.
There is admin CMS side site which is served from - /backend/public - this doesn't work
If I go to /cms then I get redirected to /cms/login which is correct. Nginx displays 404 error though which is not correct.
If I remove login requirement and go to /cms then it displays the web page as expected, but url changes from /cms to /backend/public.
I am not sure what I got wrong with the /cms and /backend/public in the nginx config.
nginx config:
server {
listen 80;
root /home/anon/web/project;
index index.php index.html index.htm index.nginx-debian.html;
server_name project.test;
location ~ ^/cms$ {
rewrite ^(.*)$ /cms/ redirect;
}
location /cms {
rewrite ^/cms(.*)$ /backend/public/$1 last;
}
location /backend/public {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php;
}
}
location / {
if (!-e $request_filename) {
rewrite ^/(.*)/$ /$1 redirect;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /frontend/public/$1 break;
}
rewrite ^/[a-zA-Z0-9_/\-'"$]*$ /frontend/public/index.php last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_buffering off;
}
}
.htaccess file I am trying to convert into nginx config:
Options -Indexes
RewriteEngine on
RedirectMatch 301 ^/cms$ /cms/
RewriteRule ^cms(.*)$ backend/public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/frontend/public/%{REQUEST_URI} -f
RewriteRule ^(.+)$ frontend/public/$1 [L]
RewriteRule ^[a-zA-Z0-9_/\-'"$]*$ frontend/public/index.php [L]

htaccess rules to nginx rules

I need help with reqrite htaccess rules to NGINX rules
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
if any one could help me if those rules can be worked as NGINX rules
Try this:
location / {
try_files $uri/ /$uri.php$is_args$args;
}
You will need the PHP scripts handler location block!
location ~ \.php$ {
# fastcgi or other backend configuration here
...
}

Migration failed from htaccess to nginx configuration

I'm in process of migration from .htaccess to nginx.conf file but after migration rules doesn't work as I expected.
This is content of .htaccess that I'm gonna migrato to nginx.
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
And this is content of nginx.conf that I've migrated:
index index.php
location / {
rewrite ^(.*)$ /index.php?$1 break;
}
Please let me know what's wrong with this and how can I get nginx conf file works.
Regards. Yuming
Those .htaccess statements perform a similar function to the Nginx try_files directive.
Try:
location / {
try_files $uri $uri/ /index.php?$uri;
}
See this document for details.

Convert htaccess to nginx, seek solution

I have this nginx setup on all my websites. If I change this too much, usually the website does not work. The problem now is that I have a website that I bought with an htaccess file. I tried to convert using online converters, but nothing works...
MY NGINX CONFIG
server {
server_name doutor.pt www.doutor.pt;
access_log /var/log/nginx/doutor.pt.access.log;
error_log /var/log/nginx/doutor.pt.error.log;
root /var/www/doutor.pt/htdocs;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
HTACCESS:
################################
# MAIN SETTINGS #
################################
# Remove index
Options -Indexes
# Set directory indexes
DirectoryIndex index.html index.php under-construction.html parking-place.html
################################
# APACHE REWRITES #
################################
RewriteEngine On
# Domain page parser for sitemap
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sitemap-doctors-([0-9]+).xml$ index.php?page_name=sitemap&category=doctors&page=$1 [L,QSA]
# Domain page parser for sitemap
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sitemap-doctors.xml$ index.php?page_name=sitemap&category=doctors&page=0 [L,QSA]
# Domain page parser for sitemap
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sitemap.xml$ index.php?page_name=sitemap [L,QSA]
# Domain page parser for category
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^category/([0-9]+)-([^/]+)$ index.php?page_name=doctors&category=$1 [L,QSA]
# Domain page parser for doctor page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^medico/([^/]+)$ index.php?page_name=medico&doctor_url=$1 [L,QSA]
# Domain page parser for secondary level pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)$ index.php?page_name=$2&page_category=$2 [L,QSA]
# Domain page parser for regular pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !sitemap$
RewriteRule ^([^/]+)$ index.php?page_name=$1 [L,QSA]
################################
# CUSTOM #
################################
Nginx is a beautiful thing and enables almost all the time to use a location block instead of a potentially hard to follow/understand/debug rewrite. This htaccess files converts easily and i've tested a bit of it's output (result) a little while back when i was asked if i could help on some other site.
As Richard's code shows, apache's weird negative logic -d and -f translate easily to a try_files $uri $uri/ in nginx where you check the request uri against web root path.
The directory indexing is disabled by default in nginx so one must enable it to use it... nothing to add is better than (again) apache's weird negative logic. Here's what the index would look like...
index index.php index.html under-construction.html parking-place.html;
Here's what the sitemap rewrite translate to, from my understanding:
# If the URI is bang on /sitemap.xml
location = /sitemap.xml {
try_files $uri $uri/ /index.php?page_name=sitemap;
}
# If the URI is bang on /sitemap-doctors.xml
location = /sitemap-doctors.xml {
try_files $uri $uri/ /index.php?page_name=sitemap&category=doctors&page=0;
}
# Enclosing this in the ~* /sitemap block isn't mandatory but it only
# helps creating cleaner, readable code while also making sure we contain
# all /sitemap*s requests thus taking care of the !sitemap directive.
location ~* /sitemap {
location ~ ^/sitemap-doctors-([0-9]+).xml {
try_files $uri $uri/ /index.php?page_name=sitemap&category=doctors&page=$1;
}
}
Following the same logic, the next one in line would look something like this:
# It seems we are only using the first capture group ( $1 ) here so the
# regex could be modified for something simpler but to make sure we are
# not catching stuff we don't want, leaving this precision will save troubles
location ^/category/([0-9]+)-([^/]+)$ {
try_files $uri $uri/ /index.php?page_name=doctors&category=$1;
}
Again, for more of the same...
# The ^ and $ are regex delimiters for start and end, so you know...
location ^/medico/([^/]+)$ {
try_files $uri $uri/ /index.php?page_name=medico&doctor_url=$1;
}
Now that we got rid of all precise stuff, it's time for some generic uri catching and passing to php...
I'm thinking this htaccess isn't proper... I would love to get an explanation..
This here sends the capture block ( $2 ) twice which in my opinion is really unusual and doesn't make sense. That being said, without seeing it in action or looking at the source code, it's a tricky one...
RewriteRule ^([^/]+)/([^/]+)$ index.php?page_name=$2&page_category=$2 [L,QSA]
a url of http://hungry.man/delicious/pizza would be seen to php as http://hungry.man/index.php?page_name=pizza&page_category=pizza without any reference to it being delicious...
For what's left i would be tempted to just use a #rewrite and write thoses as they are, without challenging my brain too much about it.
Hope it helps, Mat
It seems to me that the .htaccess file is trying to do something like this:
location / {
try_files $uri $uri/ #rewrite;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
location #rewrite {
rewrite ^/sitemap-doctors-([0-9]+).xml$ /index.php?page_name=sitemap&category=doctors&page=$1 last;
rewrite ^/sitemap-doctors.xml$ /index.php?page_name=sitemap&category=doctors&page=0 last;
rewrite ^/sitemap.xml$ /index.php?page_name=sitemap last;
rewrite ^/category/([0-9]+)-([^/]+)$ /index.php?page_name=doctors&category=$1 last;
rewrite ^/medico/([^/]+)$ /index.php?page_name=medico&doctor_url=$1 last;
rewrite ^/([^/]+)/([^/]+)$ /index.php?page_name=$2&page_category=$2 last;
rewrite ^/([^/]+)$ /index.php?page_name=$1 last;
# some default action???
return 404;
}
The main difference between rewrites in Apache and nginx, is that the latter requires the leading / on URIs. I have not implemented the RewriteCond %{REQUEST_URI} !sitemap$ rule or implemented a final default action. You will need to determine what works: return 404 or rewrite everything to /index.php.

.htaccess to nginx rewrite rules

I want to convert a simple .htaccess file (rules) to be used with nginx. I try to access an online tool after googleit but that website is down. So, if anyone cand help, i would appreciate. Thank you. Here is my .htaccess file:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Please, try these rules:
# Deny all files that started with dot
location ~ /\. {
deny all;
}
# Try $uri - is file, $uri/ - is dir, index.html - caching, $uri.html caching, index.php -last
location / {
try_files $uri $uri/ index.html $uri.html index.php;
}
Please, try these rules:
# nginx configuration
location ~ \.html$ {
}
location / {
if ($request_uri ~ "\..+$"){
rewrite ^/$ /index.html;
}
rewrite ^/([^.]+)$ /$1.html;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
}

Resources