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.
Related
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>',
Now, my website's url looks like this because I'm using the approach described here
http://localhost:4200/#/cadastro
Is it possible to remove the hash in the url and not get the 404 error?
EDIT: Router Module added
const appRoutes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'cadastro', component: CadastroNoivosComponent },
{ path: '**', component: HomeComponent }
];
export const routing = RouterModule.forRoot(appRoutes);
If you are using Angular final, the reasons to the hash could be:
RouterModule.forRoot(yourRoutesHere, { useHash: true })
So by removing that could help.
RouterModule.forRoot(yourRoutesHere)
Alternatively if you in your providers (in NgModule) have used:
{provide: LocationStrategy, useClass: HashLocationStrategy}
just remove that.
EDIT, if you need LocationStrategy, try changing HashLocationStrategy to PathLocationStrategy:
{provide: LocationStrategy, useClass: PathLocationStrategy}
More about LocationStrategy here
Now that I have seen your routes as well regarding your 404 issue, you could try changing the following
{ path: '**', component: HomeComponent }
to:
{ path: '**', redirectTo: '', pathMatch: 'full' }
More about routing here
Also check that in your index.html you have set the basehref like so:
<base href="/">
If you use PathLocationStrategy as describe here you can remove the hash in the URL.
But getting rid of 404 error needs some server side tweak. A quick and easy way is to configure your server to load the home page when any URL of the form http://yourhost/* is requested.
Create a .htaccess file Paste the following Code And Upload on your prod Server.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
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 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>',
],
],
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>',),