I have a website
https://dev.mywebsite.com/index.php?album=portraits
which displays photo-albums dynamically based on a POST value.
I want to rewrite this URL to this:
https://dev.mywebsite.com/portraits
But my Nginx rewrite rule is not working. Nothing is happening when I enter https://dev.mywebsite.com/index.php?album=portraits. And no page is found when entering https://dev.mywebsite.com/portraits.
I don't know what I am doing wrong.
This is the code I'm trying to use currently:
location / {
rewrite ^/(.*)$ /album.php?album=$1 last;
}
I've also tried this:
location = /portraits {
rewrite ^/portraits?$ /index.php?album=portraits break;
}
and this:
location = /album {
rewrite ^album/([a-z]+)/?$ album.php?album=$1 break;
}
This is the entire nginx site-config file i'm using:
server {
listen 80 default_server;
listen 443 ssl;
root /config/www;
index index.php index.htm index.html;
server_name dev.mywebsite.com;
ssl_certificate /config/keys/cert.crt;
ssl_certificate_key /config/keys/cert.key;
client_max_body_size 0;
location / {
try_files $uri $uri/ /index.html /index.php?$args =404;
}
location / {
rewrite ^/(.*)$ /album.php?album=$1 last;
}
location ~ \.php$ {
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
If the file you really have is index.php, then you can rewrite (without any additional location) with:
rewrite ^/portraits$ /index.php?album=portraits last;
Keep the location ~ \.php$ { of course :)
Related
I have set the two Sub domain for front end and back end code. I want to redirect the new domain name if user hit the old domain. But I am getting error- "414 Request-URI Too Large". I am using Nginx server for back end code.
server {
root /var/www/html/frontendCode/dist;
index index.php index.html index.htm;
server_name dev.olddomain.com;
location / {
try_files $uri $uri/ /index.html;
}
rewrite ^ $scheme://dev.newdomain.com$request_uri permanent;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name devapi.olddomain.com;
root /var/www/html/apiBackendCode/public;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
rewrite ^ $scheme://devapi.newdomain.com$request_uri permanent;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}
location ~ \.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;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
}
}
you can redirect using this:
server {
listen 80;
server_name dev.domain1.com;
rewrite ^/(.*) https://dev.domain2.com/$1 permanent;
}
Try this
server {
server_name .mydomain.com;
rewrite ^ http://www.adifferentdomain.com$request_uri? permanent;
}
you can try redirecting like that.
return 301 http://dev.newdomain.com$request_uri;
I'm on a NGINX web server and I would like to remove the .php extension from url.
I currently have the following conf :
server {
server_name www.mywebsite.com mywebsite.com;
return 301 https://mywebsite.com$request_uri;
}
server {
listen 443 ssl;
server_name www.mywebsite.com;
return 301 https://mywebsite.com$request_uri;
ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
}
server {
listen 443 ssl;
root /opt/http/nginx/sites/mywebsite/www;
index index.php index.html;
server_name mywebsite.com;
location / {
#rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
try_files $uri $uri/ $uri.php?$args;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~* \.(pdf)$ {
expires 30d;
}
client_max_body_size 3M;
ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
error_page 403 /index.php;
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;
}
access_log /var/log/nginx/www.mywebsite.access.log;
error_log /var/log/nginx/www.mywebsite.error.log;
}
If tried to follow some similar case instructions but nothing seems to work with my conf.
The problem is that this piece of code
location / {
try_files $uri $uri/ $uri.php?$args;
}
is working well in two cases :
The client asks for https://mywebsite.com/page.php : OK
The client asks for https://mywebsite.com/page : OK
without rewriting the url !
What I need is to tell NGINX to rewrite the url if the client tries to access a page with the file extension. For example, if I ask for login.php, nginx rewrites 'login', and so on.
It also has to keep the GET params in the url if it has.
So what is the right conf to do that, given that I keep in my code the links to the php files with extension and not the relative urls (I hope NGINX could rewrite them) ? (if I need to set the relative urls in my code I can but I don't want to break my local)
Thanks to this question and the comments, I was able to figure out a similar issue, same result with some additional constraints like not using ifs or try_files. Here's what I got at the end:
upstream php {
server unix:/var/run/php-fpm/php-fpm.sock;
}
server {
# ...
rewrite ^(/.+)\.php$ $scheme://$host$1 permanent;
location / {
rewrite ^(.*)$ /$1.php;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_pass php;
}
Major contributors were the lack of break/last in the rewrites.
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
This will remove both html and php form url .More Detail
I tried too many options until one of them worked for me, I was able to remove the extension with this:
server {
listen 80;
server_name www.example.local;
root /var/www/vhosts/example/httpdocs;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ #ext;
}
location ~ \/\.php {
rewrite "^(.*)\/.php" $1.php last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location #ext {
rewrite "^(.*)$" $1.php;
}
}
I use Nginx with my webserver, and don't fully understand how the rewrite works here. I got it working on Apache.
I use MVC based PHP project, and i want to use clean URL's as i have done with Apache.
www.domain.com/index.php?url=home/index works fine.
www.domain.com/home/index does not work.
As you can see, i want to have clean URL's like the last one.
I've tried several rewrites, and don't know if I should use try_files or rewrite. But I guess it's rewrite im after.
As I said, I'm still learning the rewrite.
My server block looks like this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html/pub/;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/index.php?$arg_url /$arg_url permanent;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
I thought my rewrite block made sense, but I guess it doesn't. Tried several rewrites, either I get 500 or 404 error. And as I said, I don't get rewrites yet. Need some help to get started.
Thanks in advance.
Not an expert with nginx either but I think you are looking for something like this:
location / {
index index.html index.htm index.php;
if (!-d $request_filename) {
rewrite ^/(.*)$ /index.php?url=$1 last;
break;
}
}
Then you can remove that rewrite directive
The rest of my config looks like this:
location ~ \.php$ {
root /var/www/path;
fastcgi_split_path_info ^(.+\.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;
fastcgi_buffer_size 128k;
fastcgi_buffers 2564k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
location ~ /\.git {
denyall;
}
#Staticfileslocation
location ~* ^.+.(swf|jpg|jpeg|gif|png|ico|css|bmp|js|zip)$ {
expires max;
root /var/www/path;
}
Try with this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html/pub/;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/(.*)$ /index.php?url=$1 last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
We are introducing a new section on our website which uses the Laravel framework. It is placed in a sub-directory, for example /newsection. How should I configure the nginx.conf without making any conflicts with my previous rewrite rules.
This is my current nginx.conf
server {
listen 80;
server_name localhost www.website.com;
root /home/www/website;
index index.html index.php;
location /newsection/ {
rewrite ^/ /newsection/public/index.php last;
# this is my attempt at it
}
location / {
try_files $uri $uri/ #rewrite;
}
location /php-status {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
location #rewrite {
rewrite ^/([\w_-]+)/?$ /index.php?page=$1 last;
rewrite ^/([\w_-]+)/([\w_-]+)/?$ /index.php?page=$1&slug=$2 last;
rewrite ^/([\w_-]+)/([\w_-]+)/([\w_-]+)/?$ /index.php?page=$1&slug=$2&pagination=$3 last;
}
include php.conf;
}
If it works like most frameworks I know work, this should work
location /newsection/ {
try_files $uri $uri/ /newsection/public/index.php$request_uri;
}
If this returns 404 try removing the extra /newsection in the fallback URI of the try_files
Try this for laravelin subdirectories
location ^~ /laravel {
alias /var/www/laravel/public;
try_files $uri $uri/ #laravel;
location ~ \.php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
}
}
location #laravel {
rewrite /laravel/(.*)$ /laravel/index.php?/$1 last;
}
I configured my NGINX for Zend in the following way (PHP 5.3 with fpm):
server {
root /home/page/public/;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Now i want to process additional get params like: http://web.site/index?par=1
WIth my local dev system (Apache) it works fine but not under NGINX which did'T deliver the get params.
Anny suggestions?
Edit:
Now i use the following config which seems to work but i'm not happy with it since everybody suggests "use try_files whenever possible".
location / {
if (!-e $request_filename) {
rewrite /(.*)$ /index.php?q=$1 last;
break;
}
}
From Nginx docs ( http://wiki.nginx.org/HttpCoreModule#try_files):
If you need args preserved, you must do so explicitly:
location / {
try_files $uri $uri/ /index.php?$args;
}
I use a rewrite module for this; try replacing your location / block with the following:
location / {
index index.php index.html index.htm;
}
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}