1

My goal is write web app using Yii-2. I'm use apache as my local web server on ubuntu. Currently, I want to configure yii2 urlManager. And write this in config/web.php:

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        'enablePrettyUrl' => true,
        'showScriptName' => false,
    ],

This is simple setup. Where enablePrettyUrl work fine and i can use url-link such as http://localhost/basic/web/index.php/nameOfController/AndAction. But when i setup showScriptName this does not work correct for me. I configure apache as:

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

And to of course I added .htaccess file into web folder, with this:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

I use basic yii2-basic application and after run server, I can open site for example as: localhost/basic/web/testController/testAction. But i want use link like this: localhost/basic/testController/testAction, at last example i dont use folder web.

Okay, thanks all who read it. And have you any idea how i can fix this? Thanks. And mod_rewrite is enabled too.

1 Answers1

1

On development environment, you should simply point web server to web directory, in current state it's pointed to project root directory which is located one level above. With Apache it can be done by changing DocumentRoot, see this answer.

If you have possibility to do the same on production server, it's OK. If not (for example it's shared hosting), here is what you need to do.

First of all in official docs there is dedicated section for deploying and configuring application on shared hosting.

Also this question was asked many times before, these are links for them on SO:

From my personal experience of configuring Yii2 on shared hosting, I solved this with symlinks.

For example hosting provides path for project something like /home/user_name/projects/project_name

Webroot is /home/user_name/projects/project_name/htdocs and changing it is not allowed. I placed project in separate folder: /home/user_name/projects/project_name/current. Then I executed following commands in terminal:

cd ~/projects/project_name
rm htdocs
ln -s current/frontend/web htdocs

cd ~/projects/admin-project_name
rm htdocs
ln -s ../project_name/current/backend/web htdocs

admin-project-name is separate project for backend.

This is for advanced application, for basic application it's just:

cd ~/projects/project_name
rm htdocs
ln -s current/web htdocs
Community
  • 1
  • 1
arogachev
  • 33,150
  • 7
  • 114
  • 117