Problem in converting .htaccess mod_rewrite to nginx directive - php

I have great difficulties converting this apache rewrite rule to nginx:
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks
Options -Indexes
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . index.php [L,QSA]
</IfModule>
What I've tried
A:
location / {
try_files $uri $uri/ index.php$is_args$args;
}
B:
location / {
try_files $uri $uri/ index.phps$args;
}
However A does give 404 error for any /path and B works for /path/to/somewhere but does downloads /path.
here is my complete vhost:
server {
listen 8000;
root /var/www/myphpapp;
index index.php index.html index.htm index.nginx-debian.html;
server_name localhost;
#autoindex off;
location / {
try_files $uri $uri/ index.php$is_args$args; #A
#try_files $uri $uri/ /index.php?$args; #B
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
So I'm wondering what is the correct nginx directive?

I think there is a typo. You missed a slash:
try_files $uri $uri/ /index.php$is_args$args;

Related

Lumen Route redirect to 404 in Nginx

I am doing one project in lumen, and I have installed this in my LAMP server. I have use a htaccess file to strip index.php from url.
here is my htaccess file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Here everything is working fine.
Now I am moving my files to newly created instance with LEMP stack server (nginx). This is lumen file, so I have installed composer in my project directory.
when I am putting a test route in url (browser), (eg: website.com/getUser) it is showing 404 page error.
SO I have modified the nginx default file in /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
#try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi-php.conf;
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
and then I have created a separate server block file for my website by copying the default file and making some changes. that file is below.
server {
listen 80 ;
listen [::]:80 ;
root /var/www/html/my.website.com;
server_name my.website.com;
location / {
rewrite ^/(.*)/$ /$1 redirect;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Now, when I typing my url (website.com/getUser), then it is downloading something. after opening in sublime, I get to know that this file is actually index.php which is in root directory of my project (website.com/index.php)
I am not getting, why it is happening. Why I am not be able to access my route. Where is the problem, can you guys help me in this.
Thanks in advance.
Add this to your config:
location / {
try_files $uri $uri/ /index.php?$query_string;
}

Existing Apache .htaccess to Nginx Config

I think ive got lost a little bit.
I tried a few solutions, but i dont get one part. Everything has to be handled by index.php and i can not get it work in nginx. I keep learning Nginx as its awesome but would really appreciate some quick help from somebody.
My .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_route_=$1 [L,QSA]
And my Nginx config is :
server {
listen 80;
listen [::]:80;
root /folder;
index index.php index.html index.htm index.nginx-debian.html;
server_name hostname.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files = $uri #missing;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location #missing {
rewrite ^ $scheme://$host/index.php permanent;
}
}
Can somebody point me to the right direction? Thanks
Any URI which does not end with .php is processed by the location / block. If you want to implement your Apache .htaccess rules, that is where it should be done. See this document for more.
For example:
location / {
try_files $uri $uri/ /index.php?_route_=$uri;
}
In the location ~ \.php$ block, you should guard against passing uncontrolled requests to PHP, for example:
location ~ \.php$ {
try_files $uri =404;
...
}

Problems moving Sites from Apache to Nginx (mod_rewrite)

I am migrating some vhosts from apache to nginx. Unfortunately i am having some troubles because of .htaccess files from the apache instance.
Here is the situation (apache):
The DocumentRoot has different folders with projects inside. I could access most of them (through www.example.com/appname) without problems.
But on accessing (the main page) www.example.com the following .htaccess file redirects the users.
/var/www/html/.htaccess:
Redirect /index.html https://example.com/example/
Which then has another .htaccess in /var/www/html/example/.htaccess
Options -Indexes
IndexIgnore */*
Options FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)?$ web/$1
So then the next .htaccess in /var/www/html/example/web
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
How can i translate this to an nginx location block or blocks ? I can't seem to get it right. On my last check the client browser said that something is wrong with redirecting. Here is the nginx vhost config:
Please keep in mind that there are still some tries i found, but so far i didn't have any luck. Help would be really appreciated !
thank you guys
server {
server_name example.com www.example.com;
listen 80;
root /var/www/html;
index index.php index.html index.htm;
access_log /dev/stdout;
error_log /dev/stdout info;
# FIRST .htaccess FILE
# redirect index.html to example.com/example
# the first line seems to have worked, the 2nd one is not yet tested
location /index.html { rewrite ^(.*)$ http://example.com/example/ redirect; }
location /index.html { return $301 $scheme:http://example.com/example/$request_uri; }
location / {
try_files $uri $uri/ /index.php?$args /index.html;
#try_files $uri /index.php?$args /index.html;
}
location /example {
root /var/www/html;
# try_files options:
# try_files $uri $uri/ /index.php?$args;
# try_files $uri /index.php?url=$request_uri;
# SECOND .htaccess FILE
rewrite ^/(.+)?$ /web/$1;
location /example/web {
# THIRD .htaccess FILE
# option 1
# if (!-e $request_filename){ rewrite ^(.*)$ /index.php; }
# option 2
#rewrite ^/(.*)$ /$1.php last;
try_files $uri $uri/ /index.php$args;
}
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php5:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# ocurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
check this out: Apache mod_rewrite to Nginx rewrite rules.
basically, apache rewrite and nginx rewrite do not quite mean the same thing. it seems that you are using nginx rewrite where you should be using nginx try_files.
so, you might try doing
try_files /web/$uri /web/$uri/ /web/index.php?$args
instead of
rewrite ^/(.+)?$ /web/$1;

Laravel 5.1 giving 404 Not Found except Homepage

I have a website set up on Laravel 5.1. It throws a 404 error on any page except the homepage. However, when I add index.php in the URL, it works. My site runs on a Ubuntu machine with Nginx as the web server.
Loads fine: mysite.com/index.php/dashboard
Gives 404: mysite.com/dashboard
My .htaccess in the public folder:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Thanks for your time and I would greatly appreciate any help.
Edit:
This is my Nginx conf:
server {
server_name mysite.com;
return 301 $scheme://www.mysite.com$request_uri;
}
server {
listen 80;
server_name www.mysite.com;
# note that these lines are originally from the "location /" block
root /usr/share/web/site/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/web/html;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_max_temp_file_size 0;
fastcgi_buffer_size 4K;
fastcgi_buffers 64 4k;
include fastcgi_params;
}
}
A .htaccess is for Apache. It will not work on Nginx.
Try this in your nginx site configuration (taken from the Laravel documentation.)
location / {
try_files $uri $uri/ /index.php?$query_string;
}

Nginx rewrite rule for CodeIgniter

Here is the rule in English:
Any HTTP request other than those for index.php, assets folder, files folder and robots.txt is treated as a request for your index.php file.
I have an .htaccess file that works correctly on Apache server:
RewriteCond $1 !^(index\.php|assets|files|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Some correct results for this rule:
example.com = example.com/index.php
example.com/index.php/welcome = example.com/welcome
example.com/assets/css/main.css != example.com/index.php/assets/css/main.css
I tried some tools to convert from htaccess rule to nginx rule but all were incorrect.
Via http://winginx.com/htaccess (missing the exception rules for assets folder...):
location / { rewrite ^(.*)$ /index.php/$1 break; }
Via http://www.anilcetin.com/convert-apache-htaccess-to-nginx/ (error in $1 value):
if ($1 !~ "^(index.php|assets|files|robots.txt)"){
set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
rewrite ^/(.*)$ /index.php/$1 last;
}
How can I fix this? It's really hard to debug the rule.
Here is my nginx config so far:
server {
listen 80;
server_name www.example.com example.com;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
You can add this to your config:
location ~* ^/(assets|files|robots\.txt) { }
This will work correctly with your location / rule.
Your config also need to add root document and default index file.
...
root /ftp/wardrobe;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
location ~* ^/(assets|files|robots\.txt) { }
...
You want to do this instead:
if ($request_uri !~ ^/(index\.php|assets|files|robots\.txt)) {
rewrite ^/(.*)$ /index.php/$1 last;
}
$request_uri is for the original URI request by the client. If you want the URI request AFTER other Nginx rewrite rules have processed it then you would use $uri. However, for what you are trying to do the prior would be the one you would want.
Also, you need to escape special regular expression characters like . by using a backslash.
Please try this. It works for me.
server {
server_name domain.tld;
root /var/www/codeignitor;
index index.html index.php;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Categories