.htaccess RewriteRules to nginx.conf - .htaccess

I would like to convert this .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Into a nginx.conf file. So far my nginx.conf file looks like this:
server {
listen 80;
server_name _;
root /var/www/app;
index index.php;
location / {
try_files $uri /index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:8080;
fastcgi_index index.php;
include fastcgi_params;
}
}
I am getting a 502 bad gateway error.

Correct your location for compatibility with your apache rewrites:
location / {
try_files $uri /index.php?$args;
}
And error 502 means that fpm-php (fastcgi) backend is down, but this is not nginx issue :)

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]

No php extension and pretty url nginx config alternative

I just migrated my code from apache to nginx server.
What would be the alternative nginx config to my apache .htaccess.
What i use are rules for removing .php extension and pretty url rewrite.
RewriteEngine On
#remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#for pretty url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ index.php?url=$1 [NC,L]
Try this
map $uri $pretty_url {
~/(.*)$ $1;
}
server {
...
location / {
index index.php; # replace this to 'index index.php index.html index.htm;' if needed
try_files $uri $uri/ #extensionless-php;
}
location ~ \.php$ {
# default PHP-FPM handler here
...
}
location #extensionless-php {
if ( -f $document_root$uri.php ) {
rewrite ^ $uri.php last;
}
rewrite ^ /index.php?url=$pretty_url last;
}
}
Easy fix by changing one line. There's no need for extra blocks. You just need to change this one line in the /etc/nginx/nginx.conf or /etc/nginx/sites-available/your-site.com.
Change the location / directive try files to:
location / {
try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
}
Hope this works for you too :)

nginx rewirte in two different folder (folder + sub folder different rewirte)

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;
}

What is the nginx equivalent of this htaccess file?

I'm new to nginx and having a hard time trying to convert this htaccess file into readable nginx logic:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
The farthest I came up with was this:
location / {
try_files $uri $uri/ /index.php?uri=$args;
}
What am I doing wrong?
Thanks in advance
You need both URI and query string arguments, $uri and $args in nginx.
Try this:
location / {
try_files $uri $uri/ /index.php?uri=$uri&$args;
}

ToroPHP Router in nginx

I am hosting ToroPHP in /api/. My /api/.htaccess was
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]
This worked great. I converted this to the nginx equivalent which got me roughly this:
rewrite ^/api/(.*)$ /api/index.php/$1 last;
But this isn't working. What should the nginx equivalent be?
This question really belongs on http://serverfault.com, nonetheless:
Use try files
Rewriting isn't necessary, the desired results can be achieved more efficiently using try_files:
server {
...
location /api {
# try_files $uri $uri/ index.php$uri;
try_files $uri $uri/ index.php;
}
}
}
Note that it's not necessary to pass the url as an argument to index.php as it's already available as $_SERVER['REQUEST_URI'] (will require trivial modifications to index.php to work).
location /api {
try_files $uri $uri/ index.php$request_uri; # or /api/index.php$request_uri
}

Resources