how to make nginx rewirte url from one php to another php - php

I have a old web application which use get.php to interact with client app. now we upgraded our web application to use laravel framework, which use restful api as url interface,
and our server use nginx, so I want to redirect old url to new url like this:
/get.php?update => /update or (index.php/update)
I have tried with these config.
location = /get.php?update {
try_files $uri $uri/ /index.php/update;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
but it seems when I access get.php, I still get the 404 error.
So how can I redirect this using nginx's rewrite?

Nginx location block doesn't match query string, so your try will fail.
Try this:
location = /get.php {
if ($args = update) {
rewrite ^ /index.php?$query_string last;
}
}
But IF is kinda evil.

Related

how to make nginx redirect

I'm trying to do the following:
There is a site where you can find a user in the format http://localhost/username . I tried to make a rule that if there is no file on the server, then it will redirect to index.php?username=username. But the problem is that it redirects everything, even js and css. I want to do it like this, if the file was on the server, then it would access it, and if not, then it would make a request
index.php?username=username
There is this piece of code:
rewrite ^/(.+)$ /index.php?type=userinfo&username=$1 last;
Use try_files to determine if the file already exists, and branch to a named location containing your rewrite rule, if it does not.
For example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/(.+)$ /index.php?type=userinfo&username=$1 last;
}

Route all sub-locations to index.php with NGINX

I'd like to parse the URL in my php script, therefor I need everything from a specific location to route to index.php.
However, I can only see the specific php file when navigating directly to the file.
Example, the requests;
mycomain.ext/api
mycomain.ext/api/user
mycomain.ext/api/user/19
mycomain.ext/api/user/19/detail
should all route to api/index.php
Current config:
location /api {
root /usr/share/nginx/php/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$query_string;
}
Yes, because it points to /index.php and not to /api/index.php as intended. Just change your try_files to try_files /api/index.php?$args =404;. And have a dedicated /api/index.php location where you forward the request to your php-fpm.
Native there is no $query_string variable, there is only the $args variable to pass all arguments. The variable $request_uri should not be used, only if it is encoded correctly. Your php script should use explode('?', $_SERVER['REQUEST_URI'], 2)[0] to handle the correct requests.

How do I get NGINX to properly rewrite and execute on a custom PHP application?

We have a custom PHP application that we wrote and runs on Apache with .htaccess files to handle the url rewrites. We are trying to convert it to work under NGINX with FPM under Plesk Onyx.
The application generates links like:
https://somedomain.com/mypage (same as index/mypage)
https://somedomain.com/index/sitemap
https://somedomain.com/blog/some-article-name
These URL's map to index.php files that take the request_uri and use it to render the page responses.
The structure of the application is nested as follows:
docroot (/)
./index.php //handler for the request in /
./blog/index.php //handler for any request to /blog
Each index.php expects to receive a ?path={request_uri} so that it can map the request to the controllers and actions.
I have tried multiple ways to get NGINX to do this using tryfiles and rewrite, but no luck. Using rewrite I can get / to work, but it wont render /mypage or /index/sitemap.
If I try to hit /index/sitemap it downloads the index.php instead of executing it, and if I try the blog the same thing happens. In fact the only path that works is /, all others just download the index.php file.
Here is my configuration as it is now, where am I going wrong?
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control “public”;
try_files $uri #fallback;
}
location / {
#index index.php index.html index.html;
rewrite ^/([^?]*) /index.php?path=$1 break;
rewrite ^blog/([^?]*) /blog/index.php?path=$1 break;
#try_files $uri #fallback;
}
Your configuration has multiple issues. I will ignore the first location block as it seems to have nothing to do with your question.
The first rewrite will always match, so the second rewrite will never be consulted. The second rewrite will never match anyway, as nginx URIs always begin with a /. The [^?] is meaningless, because rewrite uses a normalised URI which does not include the ? or query string. Using rewrite...break means that the rewritten URI is processed within the same location, which is an error as this location is not equipped to process PHP files. See this document for more.
A solution using try_files might look like this:
location / {
try_files $uri $uri/ /index.php?path=$uri&$args;
}
location /blog {
try_files $uri $uri/ /blog/index.php?path=$uri&$args;
}
location ~ \.php$ { ... }
See this document for more.

How to configure nginx rewrite rules for fuelphp framework?

Ive faced this problem when trying to execute an action from a controller in fuelphp framework, I get an 404 message from nginx. Im able to see, for e.g. localhost/index.php or just localhost, but when I try to access to an action-controller like localhost/index.php/login/huehue I get the 404 error. Can anyone help me? this app is currently working in apache, I was facing this trouble here too but everything got fine when I executed
a2enmod rewrite
then I tried to search for equivalent config for nginx and I found this like:
location /{ try_files $uri $uri/ /index.php?$args
/index.php?q=$request_uri }
or this:
location /{
rewrite ^ /index.php?/$request_uri;}
but they didnt work for me. Ive spent several hours trying to find out the reason. This is my actual vhost file config for my site:
server {
listen 80;
listen [::]:80;
root /var/www/nginx/goutmeet;
index index.php index.html index.htm index.nginx-debian.html;
server_name goutmeet.local www.goutmeet.local;
location / {
try_files $uri $uri/ /index.php?$args /index.php?q=$request_uri #handler;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location #handler {
rewrite ^ /index.php?/$request_uri;
}
}
Id love to know how to solve this since using nginx sometimes is a better option than apache, and having this issue with fuelphp framework and not being able to use this two great tools together is awful.
Thanks in advance.
The try_files directive can have one default action. You have three! See this document for more.
Choose one:
try_files $uri $uri/ /index.php?$args;
try_files $uri $uri/ /index.php?q=$request_uri;
try_files $uri $uri/ #handler;
I do not know which is the appropriate default action for your application. They all send the request to the PHP controller, but with different sets of parameters.
The first case passes the query string; the second case passes a single parameter containing the request URI; and the third case invokes the rewrite in the named location.
I found a workaround for this problem, just add this line to config file:
error_page 404 /index.php;
I know is not the best solution but its been the only thing that has worked for me. In my opinion its acceptable since all routes should be managed by the framework and not by the web server.
Hope this helps someone.

Nameserver always at the top in url bar in nginx (with no redirect)

My app has only two pages presented to the user, both in mobile or desktop. Firstly a login page and after the app page itself. When accessing www.myapp.com the user is firstly indexed towards the login page and if he is logged in he is then php redirected to the app page. Here is my server schema:
root
index.php (login page)
mobile/mobile.php
profile/profile.php
What I'd like is to avoid the user having www.myapp.com/mobile/mobile.php,www.myapp.com/profile/profile.php but instead www.myapp.com and www.myapp.com/desktop or www.myapp.com/mobile
so he can also switch between layouts. Unfortunately this is throwing a 404 error in my servers:
nginx default file:
location ~ /mobile/mobile.php$ {
rewrite $host/mobile break;
}
location ~ /index.php$ {
rewrite $host break;
}
location ~ profile/profile.php$ {
rewrite $host/desktop break;
}
Should it be something like this or should it be another complete way? I'd like to do this in my server, not in the user device... Thank you very much for your help...
The following was tested on my development server:
server {
listen 80;
root /usr/local/www/test;
index index.php;
location = /mobile { rewrite ^ /mobile/; }
location = /desktop { rewrite ^ /profile/; }
location / {
try_files $uri $uri/ =404;
}
location /mobile {
index mobile.php;
try_files $uri $uri/ =404;
}
location /profile {
index profile.php;
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php;
}
}
It uses index and try_files to find your scripts and then internally redirect to the fastcgi handler. I have upstream php { ... } defined elsewhere. You might employ rewrite rules and the internal directive to enforce the scheme of no php script on the URL bar.
The mobile rewrite is simply to maintain consistency with the desktop rewrite, so that the client doesn't see a trailing /.
This is just one example of a multitude of solutions. Typically, there are ancillary files (css, js, images) with their URLs that also need to be accommodated.

Categories