3

Plenty of information around the net on how to hide the index.php from your Yii 2.0 application URL, however, what I'm trying to do here is to also remove the '/basic/web/' from the URL. /basic/web is the directory from which the application is running and the configuration that I have so far is the following. That goes into the config file:

'urlManager' =>[
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],

And this is my htaccess file that I have within the /web folder:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

So far, so good, I can access something directly calling mysite.com/basic/web/controller/action. What do I need to do though to remove the /basic/web so that the URL becomes just simple mysite.com/controller/action?

Any tips welcome, thanks!

EDIT: I'm looking for a way without touching the apache configuration file as I don't have access to it.

mmvsbg
  • 3,570
  • 17
  • 52
  • 73
  • Possible duplicate: http://stackoverflow.com/questions/26459414/urlmanager-not-working-in-yii-2-0/26463551#26463551 – Ali MasudianPour Nov 27 '14 at 20:43
  • Seeing that, let me edit my question as I don't have access to the apache configuration file so I'm looking for a workaround. – mmvsbg Nov 27 '14 at 20:44

3 Answers3

7
RewriteEngine on
# Change yourdomain.com to be your primary domain.

RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$

RewriteCond %{REQUEST_URI} !^/basic/web/

# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ /basic/web/$1

RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$ 
RewriteRule ^(/)?$ basic/web/index.php [L]

Change .htaccess permissions to 440 when you're done. Nothing to fear using this method, contrary to what is stated by Mihai P.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • 4
    I actually thought "The use of .htaccess files is discouraged as they can have a detrimental effect on server performance. Only use them when necessary." source: http://wiki.apache.org/httpd/Htaccess . Even if he cannot get around using a .htaccess file why put another condition in it when you can do it like I said? Isn't this a better practice (having only 1 file exposed) then what you suggest? This is the reason why Yii, Laravel, etc have 1 folder exposed to the internet and not their entire app folder. Anyway just my 2c, I would rather educate then give answers to wrong questions. – Mihai P. Nov 27 '14 at 21:53
5

You should define your apache configuration in another way. Your site should point to {folder}/basic/web and not to {folder}.

Because you changed the requirements:

For a cpanel setup you should:
1) remove the silly basic folder, what is the point of it anyway? Just because Yii installs that way does not mean you have to keep it. So move everything 1 level up.
2) Rename web to public_html make sure you rename it in some files too (config/bootstrap comes to mind).

Yes you can do it with .htaccess but you should not have the files exposed to the internet, just your web folder should be exposed so I am not giving you that solution because it is not a good one.

Mihai P.
  • 9,307
  • 3
  • 38
  • 49
  • Is there a way to do it only through the .htaccess file in case I don't have access to the apache configuration file? – mmvsbg Nov 27 '14 at 20:43
  • is this a Cpanel setup or anything similar? is this on a live server? – Mihai P. Nov 27 '14 at 20:44
  • It's shared hosting environment that doesn't allow edits to the configuration file. I do have the option to request to their support to add some custom code to it but if I have to do that for each single application, that would become quite troublesome process. – mmvsbg Nov 27 '14 at 20:48
  • It's not a cpanel unfortunately but per your suggestion, I guess I can live with just '/web' in the URL or even rename it to something more appropriate. – mmvsbg Nov 27 '14 at 20:58
  • I have not seen a hosting company that does not allow you to do what I described. If you do know one move as you will loose more time in the future going around silly restrictions. – Mihai P. Nov 27 '14 at 21:00
  • They do not have cpanel but they have their own customized hosting panel instead which is quite nice. As a result there's not public_html folder in their structure at all. – mmvsbg Nov 27 '14 at 21:04
  • They must have 1 level before the actual web folder. If they do you can do the changes I said. – Mihai P. Nov 27 '14 at 21:16
  • I think they have it the other way around. They let you in 1 level inside the web folder where you have a public folder "mysite.com" which is accessible through the web and other directories which are not. You cannot rename the "mysite.com" folder though so I don't think I can implement the changes you suggested. It's a nice tip though! – mmvsbg Nov 27 '14 at 21:26
  • rename web to misite.com – Mihai P. Nov 27 '14 at 21:30
  • At the end of the day, that remains to be the best solution. Thank you very much for the input! – mmvsbg Dec 11 '14 at 11:01
0

The Working Solution is Here!

Step 1. in yii2 basic application directory create an index.php file and fill it as below

<?php

// 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__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';

$config = require __DIR__ . '/config/web.php';

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

Step 2. Create a .htaccess file in the same base directory as follows

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