Nginx rewrite index.php?page=contact to just contact - nginx

I am having trouble with this. I want URL to be rewritten from "index.php?page=somepage" to be written to "somepage".
This is how it was in .htaccess if it helps
RewriteEngine on
RewriteRule ^(\w+)$ index.php?page=$1 [L,NC,QSA]
RewriteRule ^(\w+)+\/$ index.php?page=$1 [L,NC,QSA]
Server block:
server {
listen 80;
listen [::]:80;
server_name domain.com;
root /home/www/domain.com;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
rewrite ^/(w+)$ /index.php?page=$1 last;
rewrite ^/(w+)+/$ /index.php?page=$1 last;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Please try the following code,
server {
...
rewrite ^/index.php?page=(.*) /$1 permanent;
...
location ~ /(.*) {
try_files $uri $uri/ /index.php?page=$1;
}
location ~ \.php {
...
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
...
}
...
}

Tried using .htaccess to nginx converter and this piece of code seems to work:
rewrite ^/(w+)$ /index.php?page=$1 last;
rewrite ^/(w+)+/$ /index.php?page=$1 last;
Edit: I overcomplicated things by thinking of rewrites. The solution to what I was trying to do is this:
location / {
try_files $uri $uri/ /index.php?page=$uri;
}

Related

nginx *104 directory index of "/var/www/html/" is forbidden

I have setup the following ngnix config for my Ubuntu
server {
server_name server ;
return 301 $scheme://server$request_uri;
}
server {
server_name server;
root /var/www/server;
index index.html index.htm index.php ;
location / {
# try_files $uri /index.php?$query_string;
try_files $uri $uri/ /index.php$is_args$args;
}
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
I've looked at Nginx 403 forbidden for all files but that didn't help.
Any ideas on what might be wrong?

Nginx conf how to remove leading slash from $uri

My Nginx conf file :
location / {
try_files $uri $uri/ /index.php?url=$uri;
}
## PHP conf in case it's relevant
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include /etc/nginx/fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Trying the following URL : http://example.org/login :
expected behavior :
http://example.org/index.php?url=login
actual behavior :
http://example.org/index.php?url=/login
Use a named location and an internal rewrite. For example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/(.*)$ /index.php?url=$1 last;
}
See this document for more.

Nginx - Configuration VirtualHost for DreamVids

I'm trying to test DreamVids (https://github.com/DreamVids/DreamVids on branch v2) on my server with Nginx and PHP-FPM.
But i have a problem when i try to access it, it downloads a file.
My configuration is :
server {
server_name mydomain.fr;
root /home/dreamvids/DreamVids;
index index.html index.htm index.php;
error_page 404 index.php;
autoindex off;
location / {
try_files $uri /index.php$is_args$args;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?/$1 break;
}
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
Thanx for help
server {
listen 80;
root /home/dreamvids/DreamVids;
index index.html index.htm index.php;
server_name example.com;
location / {
try_files $uri /index.php$is_args$args;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?/$1 break;
}
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}

nginx configuration for Laravel 4

I am trying to setup my Laravel 4 project using nginx . Here is my nginx server block for laravel :
server {
listen 80;
root /home/prism/www/laravel/public;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
But my problem is , Its showing "404 not found" error for all other routes except the default one , that comes with default installation .
This is an NGINX Configuration i've used with Laravel 4 and Laravel 4.1 that works.
server {
listen 80;
server_name sub.domain.com;
set $root_path '/var/www/html/application_name/public';
root $root_path;
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
location ~ /\.ht {
deny all;
}
}
you could try this for location / { ... }
location / {
try_files $uri $uri/ /index.php?$query_string;
}
$query_string worked for me.

URL Parameters and Rewrite

I am somewhat new to nginx and am having a hard time with the rewrites. I am trying to get:
/c/545_453453_4534
to access c.php passing 545_453453_4534 in as params
Here is my current conf:
location / {
try_files $uri $uri/ #extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:8000;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
Thanks!
In your NGINX server block you need to add:
rewrite ^/c/([^/]*)$ /c.php?param=$1 last;
What I tend to do is generate my re-write Apache style then convert it to NGINX format using:
http://www.anilcetin.com

Resources