Yii2 remove index.php from url

2019-04-07 07:21发布

When I set the option in Yii to remove index.php from the URL I get a 404 error and this error in the error logs File does not exist: /var/live/var. In my browser I get this error The requested URL /var/decat/frontend/web/index.php was not found on this server. but the file is exactly in that location. What might explain that is that my document root is /var/live and decat is an alias as shown in the conf file.

This url works fine http://130.211.165.180/decat/index.php/site/login but when I remove index.php is when I get the error. I followed all the instructions to set it up in the conf file. I even tried through an .htaccess file. Here is the info from my conf file.

Alias /decat /var/decat/frontend/web

<Directory "/var/decat/frontend/web">
        # 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 
        Options -Indexes FollowSymLinks 
        AllowOverride All
        Order allow,deny 
        Allow from all 
</Directory>

标签: php yii yii2
5条回答
不美不萌又怎样
2楼-- · 2019-04-07 07:45

You should change .htaccess as

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php?r=$1 [L,QSA]

and urlManager rule as

 'urlManager' => [
    'class' => 'yii\web\UrlManager',
    // Disable index.php
    'showScriptName' => false,
    // Disable r= routes
    'enablePrettyUrl' => true,
    'rules' => array(
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ),
    ],

I am using this in my app. and it workes. my url shoes as www.example.com/user/view?id=1

查看更多
3楼-- · 2019-04-07 07:48

Add RewriteBase /decat/ after RewriteEngine on and restart apache.

查看更多
爷的心禁止访问
4楼-- · 2019-04-07 07:50

You should set Url Manager component like this:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false, // Only considered when enablePrettyUrl is set to true
],

Official docs:

查看更多
我想做一个坏孩纸
5楼-- · 2019-04-07 07:50

yii2 remove web from url

1) Edit your config/web.php file with the following at the

<?php
use \yii\web\Request;

$baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());

return [
    ...
    'components' => [
            'request' => [
                'baseUrl' => $baseUrl,
     ],
      ...
    ]
]
?>

2) Add the following htaccess to root/web

RewriteEngine On RewriteBase /

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

RewriteRule . index.php

3) Add the following htaccess to root folder where application is installed

# prevent directory listings
Options -Indexes
IndexIgnore */*

# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)?$ web/$1

enter image description here

查看更多
爷的心禁止访问
6楼-- · 2019-04-07 07:52

I think it's your Apache configuration that is wrong... You are telling it to only rewrite requests of a single character as I'm reading it?

I think it needs to be:

RewriteRule ^/(.*) index.php/$1 [L]

But I can be wrong, not an apache specialist

查看更多
登录 后发表回答