2

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

nicolascolman
  • 540
  • 4
  • 13
sam
  • 753
  • 1
  • 9
  • 32

2 Answers2

0

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

Vitaly
  • 1,191
  • 1
  • 9
  • 20
0

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>',
            ],
],
Amin Shafiee
  • 366
  • 1
  • 6
  • 19