Routing in Yii2 throws out 404-error - yii2

following routing-rules work fine under WINDOWS/XAMPP(Apache), but it fails under LINUX(nginx) by throwing out 404-Error.What are differences in defining rules in yii2 as regards these two Operation Systems?
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => true,
'enableStrictParsing' => true,
'rules' => [
'/' => 'site/login',
'reset'=>'site/request-password-reset',
'about' => 'site/index',
'contact' => 'site/contact',
'logout' => 'site/logout',
'signup' => 'site/signup',
'formular' => 'site/script',
'praktikum' => 'bewerbungen/index',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'country' => 'country/index'
],
],
.
.
.
Here is first Nginx-config-file,as deliberated:
}
location / {
# The following 2 rules are only needed with webfinger
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
try_files $uri $uri/ index.php;
}
location ~ ^(.+?\.php)(/.*)?$ {
try_files $1 = 404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$1;
fastcgi_param PATH_INFO $2;
fastcgi_param HTTPS on;
fastcgi_pass $socket;
}
# Optional: set long EXPIRES header on static assets
location ~* ^.+\.(jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
expires 30d;
# Optional: Don't log access to assets
access_log off;
}
}
server {
listen 1027;
listen [::]:1027;
set $root_path "/srv/dev-disk-by-label-appServer/Yii2_Mail/Yii2_Mail/frontend/web";
root $root_path;
index yiic.php;
set $socket "unix:/var/run/fpm-36a313b9-da05-4c39-b71e-ade398730c14.sock";
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass $socket;
}
access_log /var/log/nginx/8bdd6dde-225b-4ba0-ab28-5806c9046194-access.log;
error_log /var/log/nginx/8bdd6dde-225b-4ba0-ab28-5806c9046194-error.log;
large_client_header_buffers 4 8k;
}
root#WSL-Server:/etc/nginx/sites-available#
Here is another Nginx-config-file,as deliberated:
root#WSL-Server:/etc/nginx/sites-available#cat default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}

Related

Url Manager is not working In my yii2 applicatoin

I'm trying to set Url Manager in my yii2 basic template. Below is .htaccess file which is locate mysite.loc/web/.htaccess
Options +FollowSymLinks IndexIgnore
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php RewriteRule . index.php
and inside my web.php file I've added this code snippet:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
],
When I'm trying to enter (For example) mysite.loc/index.php/site/movies It comes error like this: 404 not found error nginx
If anybody knows share me please. What's wrong with my settings???
My config
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
config/web.php
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
Try above and go to url mysite.loc/site/movies
I've read several topics and have found correct answer. There is a difference between apache configuration and nginx configuration on setting urlManager.
If Your server Apache (as Vitaly said above):
.htaccess :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
config/web.php :
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
If your server is Nginx:
nginx congfig file (in my case /etc/ngnix/site-enabled/mysite.loc) :
server {
listen 80;
root /var/www/html/mysite.loc/web;
server_name mysite.loc www.mysite.loc;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
As you can see I have added this snippet of code and nothing more :
location / {
try_files $uri $uri/ /index.php?$args;
}
config/web.php file is same as Apache server :
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],

How to change base URL and enable prettyUrl in yii2

How can I change my base url : http://mydomin/ProjectName/frontend/web/index.php?r=product/index to something like this http://mydomin/product/index? I read many articles about this topic but none of it proof useful. I also tried the following:
(1) enable prettyUrl in my config file at frontend/config/main.php and I added some rules as I read from an article..
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'product/<controller:\w+>/<action:[\w-]+>/<id:\d+>' => 'product/<controller>/<action>',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
(2) I created a .htaccess file at my web directory at /frontend/web/.htaccess and I added the below code:
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php
Now, when I try to access http://mydomin/ProjectName/frontend/web/product/index
I got error message: The requested URL /ProjectName/frontend/web/product was not found on this server. if I comment out 'showScriptName' => false, in the urlManager setting it works in this format http://mydomin/ProjectName/frontend/web/index.php/product/index
it stripe off the ?r= but still have the index.php file..
how can I do it to hide this file and even hide all app folder like projectName, frontend,and web folder so at the end instead of http://mydomin/ProjectName/frontend/web/index.php/product/index or http://mydomin/ProjectName/frontend/web/index.php?r=product/index I will just get http://mydomin/product/index
If you use advanced-yii2 template, you must create virtual host for yor apache, like this:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName yourlocalhost.ru
DocumentRoot /var/www/yourlocalproject/frontend/web
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/yourlocalproject/frontend/web>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
yourlocalhost.ru - Your local hostname
yourlocalproject - yii project directory
And don't forget add yourlocalhost.ru to hosts file and restart apache
1- put this code in .htaccess flie in yii2advance folder (main folder of project)
# prevent directory listings
Options -Indexes
IndexIgnore */*
# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^admin(/.+)?$ backend/web/$1 [L,PT]
RewriteRule ^(.+)?$ frontend/web/$1
above code convert
'localhost/yii2advance/frontend/web/index.php'
to
'localhost/yii2advance/'
and it convert
'localhost/yii2advance/backend/web/index.php'
to
'localhost/yii2advance/admin'
2- add this code to frontend/.htaccess and backend/.htaccess file:
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
3- in backend/config/main.php put this codes:
'homeUrl' => '/yii2advance/admin',
'components' => [
'request' => [
'baseUrl' => '/yii2advance/admin', // localhost/yii2advance/admin
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
4- in frontend/config/main.php put this codes:
'homeUrl' => '/yii2advance',
'components' => [
'request' => [
'baseUrl' => '/yii2advance', // localhost/yii2advance
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],

Yii2 prettyUrl doesn't work

i have a problem..
I use yii2 starter kit with prettyUrl enabled.
It works well on my localhost.
But it doesn't work when i upload to my server.
Here's my code...
.httacces
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
base.php
...
'components' => [
'urlManager'=>require(__DIR__.'/_urlManager.php')
],
...
_urlManager.php
<?php
return [
'class'=>'yii\web\UrlManager',
'enablePrettyUrl'=>true,
'showScriptName'=>false,
'rules'=>[
// url rules
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
]
];
So far, i just disabled prettyUrl and it works fine..
Why my prettyUrl doesnt Works anyway??
Any advice?
Try to check whether the mod_rewrite is enabled in your web server. in Apache you should the AllowOverride directive should set to ALL.
you should see something like this
<Directory "/path/to/the/site/directory/">
Options Indexes
FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
Could be a path related problem
try path indipendent of the O.S. like this
...
'components' => [
'urlManager'=>require(__DIR__. DIRECTORY_SEPARATOR . '_urlManager.php')
],
...
you have wrong syntax at rules inside urlManager, it must be like:
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',),

AngularJS HTML5 nested routes

Angular: 1.1.5
I go to: http://xxxxx.com/route1/route2/route3
and /route3 gets triggered instead of route1/route2/route3
Works fine with HTML5 mode off ... :(
Nginx config
location / {
index index.html;
try_files $uri $uri/ /index.html?$args;
}
My app config
$routeProvider.when('/route1/route2/route3', {
templateUrl: '/views/home.html',
controller: 'HomeCtrl'
});
$routeProvider.when('/route3', {
templateUrl: '/views/home.html',
controller: 'HomeCtrl'
});
$locationProvider.hashPrefix('!');
$locationProvider.html5Mode(true);
$location Log:
$$protocol: "http", $$host: "xxxxxx"…}
$$absUrl: "http://xxxxxxx.com/route1/route2/route3"
$$hash: ""
$$host: "10.44.11.73"
$$path: "/route3"
$$port: 80
$$protocol: "http"
$$replace: false
$$url: "/route3"
How can I change my settings to make HTML5 urls working with nested routes?
For single page apps, this is all you need in your NGINX config:
server
{
server_name example.com *.example.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
root /var/www/html/example;
index index.html;
location / {
try_files $uri /index.html;
}
}
Add
<base href="/">
to your index.html.

nginx configuration. Yii project in separate directory [not in root]

I spent over 6 hours on this trouble.
I have nginx/1.2.7 server, and php-fpm on 127.0.0.1:9000.
I have base nginx config:
server
{
listen 80;
server_name example.org www.example.org;
index index.php index.html;
access_log /srv/www/example.org/logs/access.log;
error_log /srv/www/example.org/logs/error.log;
location /
{
root /var/www/html/example.org/public_html;
try_files $uri $uri/ /index.php;
location ~ \.php$
{
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
And it works fine! All php files work like they must.
But I have some separate yii-project, which need to execute in other than main root folder.
And I have this configuration added at bottom:
Where /srv/www/example.org/yiitest — it is a root of yiitest project (with 'protected' folder and other inside it).
location /yiitest
{
root /srv/www/example.org/yiitest;
try_files $uri $uri/ /index.php?$args;
location ~ \.php$
{
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
But it doesn't work. I've got 'File not found'.
And maximum what I can get, it is: example.org/yiitest/ the main page works fine.
And if I go to example.org/yiitest/site/contact/ I'll get file not found. :(
I can't understand, how to correctly setup yii-project in separate subdirectory of a server.
create symlink
cd /var/www/html/example.org/public_html
ln -s ../yiitest/public yiitest
configure nginx
root /var/www/html/example.org/public_html;
location / {
...
}
location /yiitest/ {
index index.php;
# front end
if (!-e $request_filename) {
rewrite ^(.*)$ /yiitest/index.php last;
break;
}
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1: 9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
include fastcgi_params;
}
Then configure yii framework. You should set 'basePath' in your config:
<?php
return array(
'basePath' => 'yiitest',
...
);