7

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://13.21.16.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>
6
  • The config looks ok. Have you restarted the apache service? Commented Jun 2, 2015 at 8:45
  • I restarted it and now I get the new error that I posted in the question Commented Jun 2, 2015 at 13:55
  • If you say that your DocumentRoot is /var/live then I don't understand why you have Alias to /var/decat and not to var/live. Try Alias /decat /var/live/frontend/web and also <Directory "/var/live/frontend/web">... Commented Jun 4, 2015 at 14:27
  • Because I don't have the folder in the document root to enhance security, that's why I'm using an alias to access the only folder that should be accessible. The folder is in var/decat, it works fine with the index.php so the alias is working correctly Commented Jun 4, 2015 at 14:28
  • Try to add RewriteBase /decat/ after RewriteEngine on (and restart apache) Commented Jun 4, 2015 at 14:43

5 Answers 5

7
+50

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

Sign up to request clarification or add additional context in comments.

Comments

7

You should set Url Manager component like this:

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

Official docs:

2 Comments

That's what I have it set as, that gets rid of the index.php and that's when I get the error
@GauravParashar He mentioned error in the very beginning of the question.
5

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

1 Comment

After these confuguration in server also we need to enable mod_rewrite. In terminal run these commands sudo a2enmod rewrite sudo service apache2 restart Now go to “/etc/apache2/apache2.conf” open it your favorite editor and change "AllowOverride none" to "AllowOverride All" save your changes and you need to restart Apache again by above command. File will be on readonly mode. So ensure the root login so add "su" command first su vim /etc/apache2/apache2.conf sudo service apache2 restart tutsnare.com/remove-index-php-from-url-in-yii2
4

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

Comments

0

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

6 Comments

I get this error with that File does not exist: /var/decat/frontend/web/site, it should be web/index.php not site, but this is the same error I was getting before restarting the apache service
I haven't used site anywhere in my answer, no idea what you are referring to.
You should open up your index.php file and put something like die("here"); at the start of it (before anything is done). If that is outputted instead of the error your apache is configured correctly and passing thing to yii.
did that and I got the same thing
Means that it never gets to Yii at all and that it is a problem in your Apache configuration. Are you sure it's reading your configuration at all? You should probably thinker a bit with the rewrite settings. It's very hard to diagnose like this.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.