Yii Framework: Yii application in a root subfolder - php

I'm trying to run a Yii application placed in a subfolder of the root:
-> public_html
----> yii_app
--------> index.php
--------> .htaccess
--------> protected
etc..
The application runs fine with url: http://www.site.com/yii_app
However, deeper controllers and actions redirect me to http://www.site.com
For example: http://www.site.com/yii_app/controller1/action1 will show the home page of the site.
I tried to put in the root .htaccess file rules that ignore the folder yii_app but didn't solve the problem.
RewriteEngine on
RewriteCond %{REQUEST_URI} "/yii_app/"
RewriteRule (.*) $1 [L]
Any suggestions?
Would really appreciate your help.
Thanks.

You forgot to set basePath in your config:
<?php
return array(
'basePath' => 'yii_app',
....
);

'basePath' => 'yii_app/protected',
Almost universal setting for basePath
'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',

You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). Once you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After you move the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details, I describe the approach fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory

Related

Install Laravel on domain path

I have created a new Laravel project (v5.5) which is related to my main website. Due to SEO-technical considerations I want the link to be like this: <mainwebsite.com/laravel>.
Both, <mainwebsite.com> and <mainwebsite.com/laravel> are deployed to an individual server. An Application Load Balancer redirects the traffic either to the main website or the new Laravel project.
The problem right now is that the Laravel app doesn't know that <mainwebsite.com/laravel> must be seen as the project's root. (The route / must go to <mainwebsite.com/laravel> and not to <mainwebsite.com>.
I've tried to add Route::prefix('laravel')->group() ... to web.php, which does fix the routes, but then the app's public dir can't be accessed.
Using relative paths like "/css/app.css" or "/laravel/css/app.css" won't fix this.
Is there a better way to set this up or does anyone know how this must be done?
The following did the trick for me.
Change the APP_URL in the .env file to http://www.mainwebsite.com/laravel
Move the contents of the public folder into a new folder inside the public folder and give it the same name as the path behind the URL (so in this case 'laravel').
/public
--- /laravel
------ /css
------ /js
------ index.php
------ ... etc...
Edit the relative paths in the index.php file:
require __DIR__.'/../../vendor/autoload.php'; &
require_once __DIR__.'/../../bootstrap/app.php'; (add an extra /../)
Set the right paths in webpack.mix.js and add the following rules to the beginning of the file to make sure relative paths in your files will be rewritten to the right dir:
mix.setPublicPath('public/laravel/');
mix.setResourceRoot('/laravel/');
That's it!
If you're using apache, setup .htaccess to rewrite the url:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/laravel/
RewriteRule ^(.*)$ /laravel/$1 [L,R=301]
</IfModule>
This should rewrite all url paths that do not start with laravel to ones that do.
For nginx, you could do the following:
location ^~ /laravel(.*) {
return 301 $scheme://$http_host/laravel$1$is_args$query_string;
}

How to deploy yii2 with a web root other than /web

I am working on a yii2 project, and would like to deploy it to an apache server where the entire website is expected to exist in the /public_html directory, and /public_html is the web root for that host. The issue is that yii2 expects the web root to be /web, and it expects most of the site hierarchy to exist outside of the /web directory.
I would prefer not to have to change the webroot to /public_html/web, and for future projects, it may not be an option. So, is there a way to place the entire site hierarchy in /public_html , and still have the site work correctly?
I have tried creating a /public_html/.htaccess file with the following contents:
RewriteEngine on
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
And this seemed to work correctly, at first. Nothing outside of /public_html/web was accessible, and files within subdirectories of /web, such as /web/js/signup.js which could be accessed via the url http://localhost/js/signup.js, but this caused my routing rules (which were set up on a local system where I could have the web root pointing at /web) to no longer be recognized.
So, I would be happy with either an alternate to the .htaccess route, or a suggestion to how to fix the routing rules so that the .htaccess route would work.
Edit for Clarification
I have a hosting provider where the web root is ~/public_html . Yii has documentation explaining that you can rename www to public_html, and have everything that would normally be outside of /www located in ~, along with the various dotfiles and user-documents that Linux normally places in ~. I would prefer to have all site-related directories together (under the webroot).
As an alternative, a two-directory structure, such as the following would work:
~/
|--- (Various dotfiles and subdirectories unrelated to the web site)
|--- yii - Contains all subfolders that should be outside of the web root
|--- commands
|--- config
|--- controllers
|--- (etc...)
|--- public_html - Contains everything that can be within the web root.
|--- (etc...)
This has been asked so many times that they made a page for it in the Yii2 docs.
I personally answered this too many times to remember.
This can be done via the use of aliases.
Move your web directory to the new webroot (assuming ~/mysite.com is your existing site):
rm -rf ~/public_html
mv ~/mysite.com/web ~/public_html
Then, you move everything that was outside of your webroot to the new directory:
mv -prf ~/mysite.com ~/mysite.example-external
At this point, you should have a web root at ~/public_html , and you should have everything else in ~/mysite.example-external. You then edit your ~/public_html/index.php file. Please note that this is not considered part of yii core, so it's ok to modify it. The new index should have some variation on this:
<?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');
$thepath = __DIR__ . '/../mysite.example-external';
require( $thepath . '/vendor/autoload.php');
require( $thepath . '/vendor/yiisoft/yii2/Yii.php');
$config = require( $thepath . '/config/web.php');
(new yii\web\Application($config))->run();
From here, you also have to add aliases to your ~/mysite.example-external/config/web.php file, so yii can find its core files. In the web.php, at the point where $config is assigned a value, modify it to look something like the following:
$config = [
'aliases' => [
'webroot/assets' => '/Users/XXXXXX/mysite.example-external/web/assets',
'root' => '/Users/XXXXXX/mysite.example-external/web',
],

Codeigniter: Unable to access the Stylesheets

I have the following directory structure of Codeigniter Folder. You can see that I have put all my assets in the assets folder at the root of the application.
Base URL is defined as
$config['base_url'] = 'http://kamran.dev/Codeigniter/application/';
I tried modify it to
$config['base_url'] = 'http://kamran.dev/Codeigniter/';
but that didn't work either.
Can anyone please have a look and tell me what I'm doing wrong here?
P.S. Then I removed the .htaccess file from the application folder that contains
Deny From All
And this worked for me. But that doesn't seem like a good idea.
You have multiple options here. The easiest one is to move the assets folder to the same level as the application so that your directory structure is:
application
system
assets
css
img
js
Because the assets folder is not in a folder with the .htaccess with Deny From All it won't be blocked by your web server.
Alternatively you can add an .htaccess file in the assets folder with the following rule:
# /assets/.htaccess
Allow from all
I haven't tested this but I think it should work.
Another option is to change the .htaccess and put an exception on the assets folder but it's a very bad idea (with security implications) to remove the Deny From All without writing alternate rules to lock all folders that shouldn't be accessible.
Also, set your base_url to the folder containing application, system and assets directories (this folder contains Codeigniter's bootstrap/front controller index.php).
Additionally, I would advise you to echo your urls using the base_url() function:
base_url("assets/css/bootstrap.min.css");
OR to call a controller/route
base_url("controller/method");
The base_url function takes into account the value of $config['index_page'] as well as the necessary leading/trailing slashes.
Change your base_url to the following:
echo base_url("assets/css/file-name-here.css");
You want to pass the URL string to the base_url function. This way, you can change $config['base_url'] to whatever you want and it will append the string to that URL properly.
As an addition to the answer of grim. If you have your rewrite engine on you should have the following in your root .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>

How to route request to codeigniter project outside document root

I have the following shared hosting file structure using a codeigniter project:
myTLD.com/sites/mysite
mysite contains: application, system , index.php ... ( standard CI2 setup )
myTLD.com/public_html - contains : index.php
I have symlinked myTLD.com/public_html/index.php to myTLD.com/sites/mysite/index.php
Unfortunately I am getting:
Your system folder path does not appear to be set correctly. Please open the following file and correct this: index.php
I have set it up this way to avoid placing the actual site in the document root for security purposes . I don't want to change mysite/index.php because I want to keep the entire project in its mysite directory where it can easily be revised etc.
The application and mysite/ folder are set to 755 so I don't think this is a permission problem .
My myTLD.com/public_html/.htaccess folder directs all requests to index.php:
RewriteEngine On
RewriteRule ^(.*)$ index.php
Can someone advise me on an approach to sending requests through to the codeigniter index file without causing this error?
Thank you
You can try following way
1) Remove the symlink
2) Use this in htaccess at myTLD.com/public_html/.htaccess
RewriteEngine on
RewriteRule ^(.*)$ myTLD.com/sites/mysite/index.php?/$1 [L]
Use absolute system path if you are aware of it.

.htaccess redirect folder

All,
I want to redirect all traffic that comes to http://mysite/ to http://mysite/public folder. Currently I am doing this using the below in an .htaccess file and it works for the root directory. But if I browse to http://mysite/application, it doesn't redirect and shows the directory listing. I want all traffic regardless of what folder it is to redirect to http://mysite/public folder
RedirectMatch permanent ^/*$ /public/
Thanks
Try this mod_rewrite example:
RewriteEngine on
RewriteRule !^public/ /public%{REQUEST_URI} [L,R=301]
I see that you're using Zend Framework. Zend uses 'public' as the exposed web folder, but you should use the folder dictated by your web host. What is the name of the server-side folder that is exposed to the web? Your file structure should be like this:
application/
[web_exposed_dir]/
library/
In my case, the [web_exposed_dir] is called 'content'. You won't have to redirect everything to 'public' if you do it the way I've outlined.

Categories