0

In web.php i have this

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
        ],
    ],

apache for this folder configured like this php.conf file

<VirtualHost *:80>
AssignUserId alexzander alexzander
ServerName localhost

DocumentRoot /home/alexzander/Dropbox/study/3year/2/php/
<Directory /home/alexzander/Dropbox/study/3year/2/php/>
    # use mod_rewrite for pretty URL support
    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

    Order allow,deny
    Allow from all
    Require all granted
    AllowOverride All
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

when I'm try to access localhost/basic/images/list

I get The requested URL /index.php was not found on this server.

when I add index.php after basic it works localhost/basic/index.php/images/list

How do i get pretty url working? I think appache rewrite rule is not working, but don't know why.

in error.log

[Thu Jun 02 20:46:13.811518 2016] [:error] [pid 25046] [client 127.0.0.1:52450] script '/home/alexzander/Dropbox/study/3year/2/php/index.php' not found or unable to stat, referer: http://localhost/basic/index.php/images/list

but this because document root in apache looking inside /home/alexzander/Dropbox/study/3year/2/php/ I think that's ok

apache2ctl -M



AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 filter_module (shared)
 mime_module (shared)
 mpm_itk_module (shared)
 mpm_prefork_module (shared)
 negotiation_module (shared)
 php5_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 status_module (shared)

1 Answer 1

10

UrlManager..

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
        ],
    ],

For Apache server

Then add a new file with name .htaccess or edit an existing file in your project folder (not in protected).

enter image description here

Then add this code below to your .htaccess file

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

This is working for me and removed the index.php.

For Nginx Server

Check the server block in Nginx configuration as following:

server{
    listen      8082;
    server_name yii2.dev;
    access_log logs/yii2.access.log;
    error_log logs/yii2.error.log error;
    root /home/admin/web/nginx/html/basic/web/;
    location / {
            index  index.html index.php;
            if (!-e $request_filename){
                rewrite ^/(.*) /index.php?r=$1 last;
            }
    }
}

You can read the official guide of Yii 2 for nginx server here.

7
  • A .htaccess (hypertext access) file is a directory-level configuration it acts as a subset of the server's global configuration file (like one that i paste in question i named it php.conf) for the directory that they are in, or all sub-directories. So I alredy have this settings in appache, since .htacces is just extension of general config
    – Alexander
    Jun 3, 2016 at 11:23
  • localhost/basic/site/login The requested URL /index.php was not found on this server, if I add index.php localhost/basic/index.php/site/login i get yii\base\UnknownPropertyException
    – Alexander
    Jun 3, 2016 at 12:00
  • now it works, I don't know why general apache config can't do this, it seems like some sort of magic, but it worked, thx.
    – Alexander
    Jun 3, 2016 at 12:08
  • Glad that it worked for you. I did it the same way in Yii 1.1 and I was hoping it will work for Yii 2. Jun 3, 2016 at 12:09
  • but it works only without 'urlFormat'=>'path' I think it yii1 thing and it not supported on yii2 or smth, because with it i get UnknownPropertyException
    – Alexander
    Jun 3, 2016 at 12:18

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.