Yii1 wrong route when URL contains trailing slash - php

I have to support one old project which uses Yii1. I get very strange behavior. Pretty URL is configured.
When URL doen't have a trailing slash (e.g. /about or /blog/post/5) everything goes OK (the route is correct and the correct page is displayed). But the same URLs with trailing slash (/about/ or /blog/post/5/) make a wrong route. Disregarding of url I always get the route 'main/index' and index page is displayed (not redirected to, but displayed at all URLs with trailing slash).
Any ideas?

I had the same issue and the problem was in nginx config. Yii's pretty URLs require proper config to work. This was strange but the same config worked fine on one server and produced the described behavior on another server.
The problematic config:
location ~ ^(.+\.(js|css|jpeg|jpg|gif|png|ico|swf|mp3|html|eot|woff|ttf|otf|svg|zip|pdf|xml))$
{
rewrite ^(.*)/$ $1 permanent;
try_files $uri /index.php?$args;
}
The working config:
location /
{
index index.php;
if (!-e $request_filename)
{
rewrite ^/(.*) /index.php?r=$1 last;
}
}

Related

Nginx rewrite for root only when passing parameters

I want to make a Nginx rewrite rule for parameters pretending to be top-level directories. For instance, I want example.org/myval to be redirected to example.org/foo.php?param=myval, but at the top level I already have some other files (like index.php) I would like to be reachable.
My attempt is:
location = ^/(?!.*)$ {
rewrite ^/$ https://example.org/index.php break;
}
location = ^/(.*) {
rewrite /(.*) /foo.php?param=$1 last;
}
the 1st rule catching only root, the second trying to catch pretending top-levels (but failing),so all I get when accessing non-(something.php) files is a 404. Any ideas?
EDIT:
Later I have the PHP block:
location ~ ^/(index|signup|login|).php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
which I've come to know contains the try_files part (but shouldn't match anything else); I don't know where does the 404 come from.

Converting Rewrites from Apache to NGINX

I have a custom PHP application which follows the basic MVC pattern of development. The applications directory structure is as follows:
admin/
default/
index.php
In the admin is another index.php (which is how it handles requests to /admin).
In apache, this worked by putting a .htaccess file in each directory (admin and doc root) setting up a rewite and the application works. In NGINX, it doesnt seem so simple.
I can get the basic "default" application to work, by using this in my nginx.conf:
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?path=$1 last;
}
(we run on vhosts so I cannot reference location / or I get a duplicate rule error).
This gets the frontend application to work, but when I try to access the /admin portion of the application the login screen loads, but when I try to submit and it tries to hit the endpoint 'admin/index/index' it fails, as my rewrite rule doesnt work. Here is what I have for the rewrite in NGINX:
location /admin {
try_files $uri $uri/ /admin/index.php?path=$uri&$args;
}
I think the issue is that the $uri being passed in is /admin/index/index instead of it being what it should be and is under Apache: /index/index.
Can anyone help me correct these NGINX rules so that my application works properly?
Thanks in advance.
As you said, you pass in the extra $uri. Try this:
location /admin {
try_files $uri $uri/ /admin/index.php?$args;
}

Lumen application work with trailing slash using Nginx server

all I am new to Lumen cause I was using something else before. I have tested Lumen project and find out that it works well with nice support and documentation and can be extend to Laravel which is a bigger project. So I decide to use Lumen for a company,
The only problem I am currently facing now is trailing slash of url on Nginx.
e.g.
$app->get('welcome', function() { return 'Hello'; });
it responds to
http://mysite.dev/welcome
but with a trailing slash
http://mysite.dev/welcome/
the site throws a 404.
This is because the old website been using all url with end slash for e.g. PPC, SEO ... more, They do not want to redo and change the whole process including 3rd party who use these url and they can not do 301 from url with ending / to redirect to url without ending / which will cause too much redirection.
I have tried searching for solution for whole week now but still can not find any solution that best match this user requirement.
Is there any way for lumen to revert routing url to work with ending / and not working with without ending / ??
or otherwise could you please recommend me to use something else? To match this requirement.
I also tried this service provider still not working link
Regards
Lumen and Laravel should be fine with trailing slashes.
The 404 is likely resulting from a bad nginx configuration.
This is how my nginx config looks like to catch the trailing slash and use the same content at that location.
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$query_string;
}
If you're using Nginx, try this config:
index index.html index.htm index.php;
location #rewrite {
rewrite ^/(.*)$ /index.php;
}
location / {
try_files $uri $uri/ #rewrite;
}
I once encountered a similar problem,after debugging with RoutesRequest.php, I discovered that the 5.4 version no longer needs the $query_string or $args things(have no reason why most tutorials still have them).
Hope this helps.If not, maybe you could try debugging with RoutesRequest.php too, adding some helpful outputs will certainly help you locate the problem.

nginx redirect loop, remove index.php from url

I want any requests like http://example.com/whatever/index.php, to do a 301 redirect to http://example.com/whatever/.
I tried adding:
rewrite ^(.*/)index.php$ $1 permanent;
location / {
index index.php;
}
The problem here, this rewrite gets run on the root url, which causes a infinite redirect loop.
Edit:
I need a general solution
http://example.com/ should serve the file webroot/index.php
http://example.com/index.php, should 301 redirect to http://example.com/
http://example.com/a/index.php should 301 redirect to http://example.com/a/
http://example.com/a/ should serve the index.php script at webroot/a/index.php
Basically, I never want to show "index.php" in the address bar. I have old backlinks that I need to redirect to the canonical url.
Great question, with the solution similar to another one I've answered on ServerFault recently, although it's much simpler here, and you know exactly what you need.
What you want here is to only perform the redirect when the user explicitly requests /index.php, but never redirect any of the internal requests that end up being served by the actual index.php script, as defined through the index directive.
This should do just that, avoiding the loops:
server {
index index.php;
if ($request_uri ~* "^(.*/)index\.php$") {
return 301 $1;
}
location / {
# ...
}
}
Try that
location ~ /*/index.php {
rewrite ^/(.*)/(.*) http://www.votre_domaine.com/$1 permanent;
}
location /index.php {
return 301 http://www.example.com/;
}
If you already have first line mentioned below in your Nginx configuration file you don't have rewrite it again.
index index.php index.html index.htm;
rewrite ^(/.).html(?.)?$ $1$2 permanent;
rewrite ^/(.*)/$ /$1 permanent;
try_files $uri/index.html $uri.html $uri/ $uri =404;
This will remove .html from the URL and additionally will also remove "index" from home page or index page. For example -
https://www.example.com/index will be changed to https://www.example.com
Try
location = /whatever/index.php {
return 301 $scheme://www.example.com/whatever/;
}
Another benefit from doing it this way is that nginx does a return faster than a rewrite.

Default controller name is removed on browser refresh (CodeigniterPHP/Nginx issue?)

For all pages in my codeigniter app except my default controller, main.php, when I refresh the browser the url isn't affected as one would expect.
However when I refresh the browser at "http://localhost/main", the main part is stripped off the url. So the browser bar shows just "http://localhost".
Totally lost on where to start with this but was just wondering if anyone has come across this before...?
Here's what I think could be the relevant part of my nginx.conf (if Nginx is the problem).
if ($request_uri ~* ^(/main(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
Note that changing the bracketed code to this:
rewrite ^(.*)$ /main permanent;
results in the error message The webpage at http://localhost/main has resulted in too many redirects.
Ok I answered my own question. Simply commenting out the nginx.conf code that I showed corrects the problem.
So just do this (says Bo Jackson):
#if ($request_uri ~* ^(/main(/index)?|/index(.php)?)/?$)
#{
# rewrite ^(.*)$ / permanent;
#}
If your trying to route your request to the index.php bootstrap so you can remove the index.php from your url in CodeIgniter you probably should try using this:
location / {
try_files $uri $uri/ /index.php?$query_string;
}

Categories