Nginx remove php extension and index from url - nginx

Hope someone can help me.
be aware; sorry for possible bad english ;)
I'm trying for hours to setup my configs so my site is accessible without php extension, but will redirect any request with extension and in addition remove the /index from URL.
i have come to the point where every extension will removed and still got parsed by php5-fpm.
here is my current code:
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.php;
<<unrelated Modulecode>>
rewrite ^(.*).php(.*)$ $1$2 permanent; # << forces index to be appended, so not exactly wanted
#rewrite ^/index$ / permanent; >> results in infinity loop for indexpage
# location ~ ^(.*)(?<!(index)).php(.*)$ { # << even don't know how to at least except index...gwarz regex x|
# rewrite ^(.*).php(.*)$ $1$2 permanent;
# }
location / {
try_files $uri $uri/ #phprule;
}
location ~ \.php$ {
try_files $uri $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;
}
location #phprule {
rewrite ^(.*)$ $1.php last;
}
}
the uncommented parts are some desperate trys to get it somehow to work x<
so what i want to accomplish:
example.com/index.php => example.com/
example.com/foo.php => example.com/foo
example.com/some/path/foo.php => example.com/some/path/foo
hope you will get the idea.

Probably found the issue or moved on, but for the benefit of those who come along after, these little changes did the trick for me:
location #phprule {
rewrite ^/(.*)$ "/$1.php" last;
}
/ added to the regex, quotes and a forward slash added to the rewrite string.

Related

Location blocks in nginx not getting desired outcome - maybe ordering?

I've got a website using nginx that's got a load of redirects to retain the juice and UI that old backlinks give me but they're not all working as I need them to. I suspect it could be an ordering issue but I'm not sure so I'd appreciate some help.
I'm pretty sure I'm almost there but I can't work out a way for it all to work together!
location / {
try_files $uri $uri/ #extensionless-php;
}
location #redirector {
rewrite ^(.*)$ /redirection-check/?request=$request_uri;
}
location #extensionless-php {
rewrite ^(.*?)/?$ $1.php;
}
location ~ ^([^.\?]*[^/])$ {
try_files $uri #addslash;
}
location #addslash {
return 301 $uri/;
}
location ~ \.php$ {
try_files $uri $uri/ #redirector;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php5.6-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
The way it should work is...
If someone lands on domain.com/page or domain.com/page.php then 301 to domain.com/page/
Interpret domain.com/page/ as domain.com/page.php
If domain.com/page.php doesn't exist then go to domain.com/redirection-check/?request=the404request. This is a 301/404 handler with a BUNCH of old urls in a big php array so they're not all listed in nginx config files.
You have a location ~ \.php$ block to process URIs that end with .php. This will handle both internally rewritten URIs (as generated by your #extensionless-php block) and externally presented URIs (such as in your 1st requirement).
To handle externally presented URIs differently, you need to look at the original request which is available as $request_uri.
The $request_uri contains the original request including the optional query string, and can be tested using a map or if directive.
For example:
if ($request_uri ~ ^(.*)\.php(\?|$)) { return 301 $1/; }
See this caution on the use of if.

Nginx configuration for rewrite rule

So I have this configuration at the moment in nginx:
autoindex off;
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.html index.htm index.php;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
Now, I want to get something 'special'. I want to get an url (http://project2.local/camera?camera_id=1) to be rewritten as http://project2.local/camera/1 I've tried this code;
location / {
rewrite ^/?camera\.php$ /camera/%1? redirect;
}
location /camera {
rewrite ^/camera/([^/]*)$ /camera.php?camera_id=$1 break;
}
but that downloads something empty when I navigate to that place.. What am I doing wrong here?
Try:
location /camera {
rewrite ^/camera/([^/]*)$ /camera.php?camera_id=$1 last;
}
The rewrite...break will prevent the camera.php URI being processed by the PHP location. You need to use last as per your existing configuration. See this document for details.
You also added: location / { rewrite ^/?camera\.php$ /camera/%1? redirect; }, but it seems wrong and superfluous for a number of reasons:
You cannot have two location / blocks (perhaps you meant you added the line of code to the existing location / block)
I do not recognise the %1 term (the value of the parameter would be $arg_camera_id)
It will never match anything (URIs ending with .php are processed by the PHP location)

nginx redirect all URI requests with query strings/arguments to the equivalent without

I was reading around about removing query strings/arguments from all requests by way of redirection like $1?$argument to just $1.
I tried what the documentation said in adding a ? to the end of the desired rewrite in order to remove query strings and arguments, but that had no effect.
I wish to maintain current functionality, and remove query strings/arguments from all URI requests.
What is conflicting with the documentation's suggestion of appending ?, or what is the correct solution for this?
# for removing .php from all requests
location / {
try_files $uri $uri/ #extensionless-php;
}
# rewrite to remove .php
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
# deny access to php includes
location ~* /includes/(.+)\.php$ {
deny all;
}
# redirect all instances of index.php to / in its respective directory (for example, /index.php to /, and /articles/index.php to /articles/)
location ~* \.php {
try_files $uri =404;
if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index(?:\.php)?|\.php)(\?.*)?$) { return 301 /$1$2; }
fastcgi_pass backend;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I see you're using my solution from nginx php friendly URL redirection without interfering with index.php causing /index! :-)
If you also want to get rid of the whole $args / $query_string, in addition to removing index and .php, then just omit the $2 from the provided solutions; also, you can have an extra if-condition for removing the args from the rest of your URLs, too.
index index.php;
if ($request_uri ~ ^/([^?]*?)(?:(?<=/)index(?:\.php)?|\.php)(\?.*)?$) { return 301 /$1; }
if ($request_uri ~ ^/([^?]*)\?) { return 301 /$1; }

How to get rid of app.php in Symfony2 URIs with Nginx

I'm trying to get a clear understanding of what's going on with my nginx configuration file for Symfony2, here it is:
server {
listen 80;
autoindex on;
server_name example.com;
root /var/www/example.com/web;
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location / {
index app.php;
try_files $uri $uri/ #symfony;
}
location #symfony {
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ ^/app\.php(/|$) {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
In short, I'm trying to strip app.php in every possible URI that a user can set e.g. example.com/app.php/demo or just example.com/app.php.
This config actually works for URIs like ones above, but it leads to a redirect loop in case of trying to access "root" URI example.com.
And if I remove $uri/ from try_files and leave only $uri and a fallback #symfony there, everything is working fine except I can't access any directories as they're going to be processed by SF.
I'm out of ideas, did a lot of research on how nginx and rewrites actually work, but as for now it's a dead end for me. If you can find a solution to stay with $uri/ in try_files and get out of a loop at front, please let me know.
This solution from Nginx Tips worked for me. It's almost, but not quite, the same as yours.
server {
server_name domain.tld www.domain.tld;
root /var/www/project/web;
location / {
# try to serve file directly, fallback to rewrite
try_files $uri #rewriteapp;
}
location #rewriteapp {
# rewrite all to app.php
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ ^/(app|app_dev|config).php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
error_log /var/log/nginx/project_error.log;
}

wordpress site in subdomain on nginx

I'm having trouble setting up a subdomain with nginx and some wordpress sites.
My www.jackalopegames.com domain is working, but I want to set up dev.jackalopegames.com.
Here's the config file in my sites-enabled folder:
server
{
listen 80;
server_name jackalopegames.com www.jackalopegames.com;
include /etc/nginx/fastcgi_php;
root /var/sitefolder;
index index.php;
}
server {
listen 80;
server_name dev.jackalopegames.com;
include /etc/nginx/fastcgi_php;
root /var/devfolder;
index index.php;
}
The first one server_name jackalopegames.com works, but the second one doesn't. I've looked around a bunch and I'm at a loss as to why this isn't working. Any tips would be appreciated!
Update:
I've added the following to my subdomain server {...} with no effect:
location / {
root /var/dev;
index index.php;
rewrite ^.*/files/(.*)$ /wp-includes/ms-files.php?file=$1 last;
if (!-e $request_filename) {
rewrite ^.+/?(/wp-.*) $1 last;
rewrite ^.+/?(/.*\.php)$ $1 last;
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
{
root /var/dev;
rewrite ^/.*(/wp-.*/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$/wp-includes/ms-files.php?file=$1 last;
expires 30d;
break;
}
location ~ wp\-.*\.php|wp\-admin|\.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/dev$fastcgi_script_name;
}
}
There are a bunch of things that you may or may not have already checked. I had similar issues myself when I first did this. So, to help with just thing thing, I wrote up a walkthrough for how to do it. You can find it here: http://blog.phpadvocate.com/2014/10/setting-up-your-wordpress-blog-as-a-subdomain-with-nginx/

Resources