How to change base URL and enable prettyUrl in yii2 - 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>',
],
],

Related

Remove Question mark in url yii2?

how to remove ? in url yii2?
in rule :
'rules' => [
'<alias:\w+>' => 'site/<alias>',
],
in .htaccess
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
Try this set of rules:
'<alias:\w+>/<id:\d+>' => 'site/<alias>',
'<alias:\w+>' => 'site/<alias>',

Routing in Yii2 throws out 404-error

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;
}
}

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' => [
],
],

Yii2 Advance Application Remove '/web' and 'index.php' in frontend and backend

I referred the below given link
Yii2 htaccess - How to hide frontend/web and backend/web COMPLETELY
Remove index.php from url after removing web folder from frontend and backend in yii2
but, i did't get the output
show below URL
localhost/yii2advance/backend/web/index.php?r=site%2Flogin
localhost/yii2advance/frontend/web/index.php?r=site%2Flogin
in above url i remove /web/index.php in both frontend and backend
I get URL like
localhost/yii2advance/backend/site/login
localhost/yii2advance/frontend/site/login
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/web/.htaccess and backend/web/.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' => [],
],
4- in frontend/config/main.php put this codes:
'homeUrl' => '/yii2advance',
'components' => [
'request' => [
'baseUrl' => '/yii2advance', // localhost/yii2advance
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [],
],
Create a ".htaccess" file in the "web" directory with:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
My pull request was accepted about this. It will be in the 2.0.19 version.

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>',),