showScriptName - false gives '404'

I started out with the Yii2 “advanced” template and I finally got pretty urls to work, along with hiding “index.php”. It was pretty frustrating because there are a lot of “answers” on the web to this and none of them worked for me. Then I read “Yii2 Quickstart Guide” by JB McKee and the clear example worked for me. So now I’ve installed the “basic” template, assuming that I could simply copy my “.htaccess” commands from the other template, along with my “config” parms.

Nope. I cannot get the “basic” template to hide the “index.php” file. If I set ‘showScriptName’ => false, then none of the menu items work except for the “My Company” one, whose URL looks like ‘localhost/yiibasic’. The “Home” one doesn’t work, I guess because it’s using ‘localhost/yiibasic/index’ – I don’t know.

If I set ‘showScriptName’ => true or comment it out, then everything works fine – except I have to look at “index.php” in every URL.

Here are the htaccess options (the recommended ones) that I have:

RewriteEngine On

RewriteBase /yii_basic/ (Note that yii_basic is my real dir name and I have aliased it in my httpd.conf)

#RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

… and here are the config/web.php parms I have (again, the ones I saw recommended)

    'urlManager' => [	


        'enablePrettyUrl' => true,


        'showScriptName' => false, 


        'rules' => [


        '<alias:\w+>' => 'site/<alias>',  


        ],


    ],

This is all on my localhost. I am not going to be able to edit Apache stuff on my hosting service but I can edit or create another htaccess.

I’ve seen so many strange alternatives offered on the web (stackoverflow, etc…) but I couldn’t get them to work and since my advanced template works with these, I figured my basic one should.

So, as I mentioned, this exact same set of rules and parms works just fine in the advanced template. I’d certainly appreciate any ideas on what I’m doing wrong and how the basic template might differ from the advanced in ways that may cause this. Thanks

Ok, does this help? This is my Apache conf setup for this particular directory.

Alias /yiibasic "c:/www/yii_basic/web"

<Directory "c:/www/yii_basic/web">

AllowOverride All

Options Indexes FollowSymLinks

Require all granted

</Directory>

Your <alias> rule match "word characters" only.

Does standard rules work?




            'rules' => array(

                '<controller:\w+>/<id:\d+>' => '<controller>/view',

                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',

                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',

            ),



No unfortunately it doesn’t. I do appreciate your response though! I tried this with my ‘alias’ in place, and then without the ‘alias’. Both times, I get "Object not found The requested URL was not found on this server. The link on the

<a href="http://localhost/yiibasic/">referring page</a>

seems to be wrong or outdated. Please inform the author of

<a href="http://localhost/yiibasic/">that page</a>

about the error. " . For example, the requested URL is ‘http://localhost/yii_basic/about’ or ‘http://localhost/yii_basic/site/about’ (depending on whether “alias” is in there or not), which is what we all want in a ‘pretty url’. But it doesn’t work.

I really don’t get it.

Neither do we, apparently!

Could you try again, this time using the default htaccess taken from the Yii 2 guide?

And probably also without any URL manager rules.

Thanks for your response! I am/was reading ONLY the Yii2 Guide and I had to get the info below from the Yii 1 guide.

Here is what I see in the Yii 1 Guide:


 

   RewriteEngine on

# prevent httpd from serving dotfiles (.htaccess, .svn, .git, etc.)

RedirectMatch 403 /\..*$

# 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

 

Here is what I have in my htaccess:




Options +FollowSymLinks

RewriteEngine On


RewriteBase /yii_basic/

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule . index.php



No sir, I can’t get it to work. I still get


Object not found!


The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.


If you think this is a server error, please contact the webmaster.

Error 404

I also tried “RewriteBase /”, but that didn’t work either and it just causes my Xampp Admin panel to come up(currently htaccess is in my "/web"folder). Taking “RewriteBase” out completely didn’t make it work either. I have had this htaccess file in the application root (yii_basic/) and I have had it in the ‘/web’ folder where the index.php file is, and I have had it in both of those places. I didn’t see it specified in the Guide where to put it, so I did a Stackoverflow search. I am unsure where to put it. I still see people saying to add it to both places (https://devreadwrite.com/posts/htaccess-for-yii-2-basic).

I assume you are restarting Apache each time you make a change?

I don’t get it: why not try the example in the guide first?

Guide - Installation - Recommended Apache Configuration

Edit:

When you do, please delete/rename the htaccess in the ‘web’ folder first.

That IS the first thing I did. I followed the installation instructions, used COmposer, etc… And yeah, restarted the Apache each time. Here is the only “solution” I could get to work – move the whole “basic” template to xampp/htdocs folder. The suggested htaccess then works. I’ve always put my local sites in “c:\www” and they’ve worked fine, including TWO of them that have “pretty URLs”. My location was chosen based on info I’ve seen on the Yii site numerous times -->

. One of those sites is the Advanced template; the other is a homegrown MVC.

So I guess everyone should keep in mind that if you have your site anywhere except htdocs and you find yourself spending days dealing with this issue, then put your site under htdocs. I hope someone else can save the hours I wasted on this. I’m a simple programmer and this “config” stuff is not my area of expertise or interest – I was hoping to spend time on programming and on my business, not on this kind of stuff. But it is what it is. Maybe the Guide could be modified to include a sentence or two on this. If it’s going to suggest it’s OK to put the site anywhere but htdocs, then it ought to give notice, and maybe a workaround, regarding this issue.

Thanks for everyone’s responses.

Well, it would have helped to know that fact!

You can work around this by using virtual hosts instead - that’s what I do. Much easier. :)

I have all my sites in my home directory.

Did you use ‘Alias’ or ‘Document Root’ when you tried it?

It should work with document root.

Provided that you remove the default site (if present) in httpd.conf (or what’s it called).

(And using ‘/’ for RewriteBase)

Anyway, yes: I feel your pain. Apache configuration can be testing!

Thank you, jacmoe. Sorry for not mentioning that earlier. You and others have been very helpful. I’ve learned a lot from your answers to other posts as well.

I used Alias in my httpd.conf. I couldn’t use Document Root in httpd.conf because that is “c:\xampp\htdocs” and when I DID change it to my website, then I could not get to my Xampp Admin panel – does that make sense? I don’t understand a lot of this stuff. But this whole config thing with localhost is a moot point anyway – I’m not going to be able to tweak the web server on my VPS. I CAN tweak htaccess files.

My research and a lot of examples led me to believe that “RewriteBase” was a way to change the document root. So yessir, I did try that. COuldn’t get it to work when site was outside of htdocs. I guess I could have modified my local httpd.conf Document Root for each website I test locally, but I’ve got like seven of them so that’s kind of a pain. How do you do it without losing access to the Xampp panel?

Your issue you have/had is related to Apache security - I forget which - and nothing to do with the rewrite base.

Basically says that sites need to be in the web directory and not outside it, unless you explicitly configure it.

About RewriteBase:

Say, for instance, that you put your site (as a directory) into the htdocs directory.

There, you would need to put a rewrite base of /your-site/web I think to make it work.

‘rewrite base’ is basically: “where is the site relative to the global web root?”

And, no: unfortunately, you will lose the xampp panel if you lose the default site.

The only way out of that is virtual hosts! :)

Cool, I’ll use vhosts then and try that out. I have root access on host, so I think I can modify vhosts file. Thanks for all your help! :)

See my answer here MAIN GOES ABOVE HTDOCS

I got all working within 5 minutes after the composer install. See the guide in that link, Yes it’s a laravel guide, but installing yii2 correctly is the same basic procedure. I am number 23 on the laravel leader board, I have been doing this stuff for a while now.

Just trying to help, have development correct then when you upload project, much less headache. A vh is not needed, just a correct installation.

I go rounds on laravel forum with newbees who slap all under htdocs and have nothing but trouble. READ, TRUST guide.

In fact cakephp, same guide works, just little tweaks depending on which framework.

Later I will attempt to link to a screenshot of proper folder structure.

Keep in mind that Jimgwhit2’s solution is a speciality solution that is not normally needed

Thanks. I read that a few days ago but ignored it because 1) it was shared hosting and I didn’t think it applied to VPS and 2) I couldn’t get past the mention of ‘Laravel’. I’ll take your word for it and check it out later. So on my VPS Host, the root directory is “public_html”. So you’re saying everything EXCEPT the “/web” dir goes above public_html, for security, right? FYI, although my host has a ‘www’ folder, it is simply a link to the ‘public_html’ folder. :)

See this image Image

Don’t worry, safe on google drive the link is actually https://drive.google.com/file/d/0B1_PFw--3o74ajg5MUlXeXdlMFE/view?usp=sharing

Left folder is everything above htdocs, right folder is files that was in the web folder that comes with yii2.

Then I name it yii2 as you can see, and folder above is yii2up.

Then in yii2\index.php now just tells itself where main yii2 files reside:




<?php


defined('DS') || define('DS', DIRECTORY_SEPARATOR);

// comment out the following two lines when deployed to production

defined('YII_DEBUG') or define('YII_DEBUG', true);

defined('YII_ENV') or define('YII_ENV', 'dev');


require __DIR__ . '/../../yii2up/vendor/autoload.php';

require __DIR__ . '/../../yii2up/vendor/yiisoft/yii2/Yii.php';


$config = require(__DIR__ . '/../../yii2up/config/web.php');


(new yii\web\Application($config))->run();



Notice the /../../ to resolve path.

and my htaccess




<IfModule mod_rewrite.c>

    <IfModule mod_negotiation.c>

        Options -MultiViews -Indexes

    </IfModule>


    RewriteEngine On

    RewriteBase /yii2/




    # Redirect Trailing Slashes If Not A Folder...

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteCond %{REQUEST_URI} (.+)/$

    RewriteRule ^ %1 [L,R=301]


    # Handle Front Controller...

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteRule ^ index.php [L]


    # Handle Authorization Header

    RewriteCond %{HTTP:Authorization} .

    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

</IfModule>




notice RewriteBase /yii2/ That’s the public site like


yoursite.com/yii2

Or whatever you named the folder, could be anything.

So yii2up is above htdocs, and yii2 below htdocs. Remember yii2 is contents of the original web folder that came with yii2.

This works 100%.

jacmoe

But that is the correct way to install yii2, laravel, cakephp, or any framework, the main core files out of web root. How do you place files?

Is there a way to put images in forum?

Ok, I see what you’ve done, and I appreciate this info! I know people like to do it different ways. It won’t hurt to try this because I’m sure it won’t take as long as my initial endeavour did. My website is a user-registered site except for a few items on the public front-facing side, but my Yii research tends to suggest that I do not need the “Advanced template” because I don’t have the need for any kind of major “backend” admin stuff. There is certainly no “backend stuff” right now, I can tell you!. I can incorporate my own already-written “registration” scripts into this paradigm (as long as I follow the MVC pattern). Thanks to all of you!