I have in my site a Wordpress running in the root directory, ie: mysite.com/
And I need to create a ZF Application, and host it under a subdirectory of my hosting, ie: mysite.com/backend
I did under my root directory:
% zf create project backend
It created the "backend" directory under my root, with all the project inside.
Under my root directory I have this .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Under webroot/backend/public I have this .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I'm using ZF 1.11
What I can't figure how must I configure is the .htaccess files to get this zf app working under the subdirectory.
Regards!
Currently you have a folder backend . So it looks
mysite.com/backend
Now when you create project with
zf create project <project name>
you will be getting a directory structure . From that move the index.php and .htaccess ( from the public folder ) to the backend folder.
Upload all the remaining folder to the one which is not accessible via web . ie below the web root directory ( public or what ever you call )
Change the APPLICATION_PATH in the index.php of the backend folder according to the where your project folder is now.
Its very easy with zend framework. Hope this will help you.
May be you want to look whether wordpress .htaccess will allow the directory which is already there .
I had to put this .htacess inside mysite.com/backend
RewriteEngine On
RewriteRule !\.(csv|js|gif|jpg|png|css|txt)$ public/index.php [L]
And then in my Bootstrap.php this:
public function run() {
$front = $this->getResource('FrontController');
$front->setBaseUrl('/backend');
}
So Zend can route the requests in the right way.
Related
My question is: How can i run a laravel application in a directory
(not in root) on a shared webhosting?
For some of my clients I run Laravel applications in the root folder of their selected shared webhosting and remove the /public part from the URL. To make this work i move the index.php to the root folder and change the information from the index.php to:
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
Now i know that this can cause security problems but I already have found solutions to that case for shared webhosters.
The .htaccess i use to make this change is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(\.eot|\.woff|\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1/$2 [L,NC]
But in this case i want the laravel project to be run in a directory called test.
Folder structure:
root (public_html)
|-- test (directory with laravel project)
Now I'm not sure if i can simply change the .htaccess as i'm not very skilled with .htaccess files. What changes do i have to make, to make this work?
Note my urls:
// this link will be changed to:
www.[text-placeholder].com/test/public/css/style.css
// Output above result now:
www.[text-placeholder].com/public/css/style.css
// But should be:
www.[text-placeholder].com/test/public/css/style.css
It's easy, just place the project 1 level above the public_html folder and rename the public folder itself to public_html.
P.S. This is maybe a better way, than to use .htaccess, because now you really know fore sure the files outside of public html are out of your webroot (not accessible by hackers) and with htaccess you can make a mistake and the files are exposed.
I have a Laravel project in my public_html folder. The domain is for example domain.com My .htaccess file (in public_html folder) is like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
There are the following routes:
api/licenseplate
api/calendar
auth/login
admin/settings
admin/appointments
appointment
...
So an example of a URL is http://domain.com/appointment.
Now I would like to have a wordpress website on domain.com. So when you go to domain.com you see the wordpress website. But I also want to have the urls like /appointment of my laravel project.
What's the easiest and cleanest way to do this?
You can create symlink to Wordpress public directory in Laravel folder. For example, wp:
/var
/www
/laravel
/public
/wp #(symlink to -> /var/www/wordpress/public_html)
index.php
.htaccess
/wordpress
/public_html
index.php
.htaccess
And describe Laravel routes in .htaccess. Example of code of /var/www/laravel/public/.htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{ENV:REDIRECT_FINISH} .
RewriteRule ^ - [L]
# Laravel
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} !.*\.php$
RewriteRule ^(.*)$ $1 [E=FINISH:1,L]
RewriteCond %{REQUEST_URI} ^/(api/licenseplate)(\?.*|$) [OR]
RewriteCond %{REQUEST_URI} ^/(api/calendar)(\?.*|$) [OR]
RewriteCond %{REQUEST_URI} ^/(admin/settings)(\?.*|$) [OR]
RewriteCond %{REQUEST_URI} ^/(admin/appointments)(\?.*|$) [OR]
RewriteCond %{REQUEST_URI} ^/(appointment)(\?.*|$) [OR]
RewriteCond %{REQUEST_URI} ^/(auth/login)(\?.*|$)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [E=FINISH:1,L]
# Wordpress
RewriteCond %{REQUEST_URI} !^/wp
RewriteRule ^(.*)$ /wp/$1 [E=FINISH:1,L]
</IfModule>
Code of /var/www/wordpress/public_html/.htaccess (just copy of your wordpress .htaccess):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Server Structure
.htaccess files provide a way to make configuration changes on a per-directory basis. This means that you only need to direct the request to the directory that will have rules for how to best process the request. Thus you will leave the .htaccess files that came with both Laravel and WordPress in their directories. Just create another one for the parent directory that holds them both.
The below assumes that the full path of public_html is /var/www/public_html.
Folder Structure
/var/www/public_html/laravel
/var/www/public_html/wp
/var/www/public_html/.htaccess
# Limit Access To Laravel Public Folder
<Directory "/var/www/public_html/laravel">
Order Deny,allow
Deny from all
</Directory>
<Directory "/var/www/public_html/laravel/public">
Allow from all
</Directory>
# Rewrite Requests
<IfModule mod_rewrite.c>
RewriteEngine On
# Do Not Rewrite Directory And File Requests
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ $1 [L]
# Rewrite Laravel Routes
RewriteCond %{REQUEST_URI} api/licenseplate [OR]
RewriteCond %{REQUEST_URI} api/calendar [OR]
RewriteCond %{REQUEST_URI} admin/settings [OR]
RewriteCond %{REQUEST_URI} admin/appointments [OR]
RewriteCond %{REQUEST_URI} appointment [OR]
RewriteCond %{REQUEST_URI} auth/login
RewriteRule ^(.*)$ laravel/public/$1 [L]
# Rewrite To WordPress For Everything Else
RewriteRule ^(.*)$ wp/$1 [L]
</IfModule>
Application Settings
Both Laravel and WordPress have ways to generate links to their assets (images, CSS, JavaScript).
WordPress is built to easily allow for installation to a sub-directory so you only need to change the WordPress Address and Site Address on the General Settings Screen.
Laravel assumes that it is installed to the top directory. The asset() helper is used to generate paths to asset files. So you will need to override the default function with one of your own. Here is how:
Create app/helpers.php with these contents
/**
* Generate an asset path for the application
* with the installation sub-directory
* prepended to the path.
*
* #param string $path
* #param bool $secure
* #return string
*/
function asset($path, $secure = null)
{
$path = ltrim($path, '/');
$dir = basename(dirname(__DIR__)) . '/public';
$url = app('url')->asset($path, $secure);
return str_replace($path, $dir . '/' . $path, $url);
}
Add app/helpers.php to the autoload process by editing composer.json
"autoload": {
//...
"files": [
"app/helpers.php"
]
},
If you have command line access, update PHP composer
composer dump-autoload
Here is an approach that I took when encountered a similar situation. Install your wordpress in public_html directory and preferably place your laravel application with the WP directory:
wp-admin/
wp-content/
wp-includes/
... other wordpress files ...
app/ <-- laravel application
|-> app/
|-> bootstrap/
|-> config/
|-> public/
|-> ... other laravel files and directories ...
Now to get the WordPress functions available within your Laravel
application, include wp-load.php within your Laravel installation
and then you can use the enqueueing functions from WordPress to
load the assets you need.
Best approach is to follow the Laravel Service Provider Architecture. Write a service provider for including the wp-load.php and use the boot function to load assets:
// app/Providers/WordPressServiceProvider.php
class WordPressServiceProvider extends ServiceProvider {
protected $bootstrapFilePath = '../../wp-load.php'; // Adjust ur path
public function boot() {
// Load assets
wp_enqueue_style('app', '/app/public/app.css'); // Adjust ur path
}
public function register() {
// Load wordpress bootstrap file
if(File::exists($this->bootstrapFilePath)) {
require_once $this->bootstrapFilePath;
} else throw new \RuntimeException('WordPress Bootstrap file not found!');
}
}
And add the service provider to your laravel app config, config/app.php:
/* ... */
'providers' => [
// ...
/*
* Application Service Providers...
*/
// ...
App\Providers\WordPressServiceProvider::class,
Now we have access to our WordPress functions within Laravel and can alter laravel views to something like:
{!! get_header() !!}
<div class="container">
<div class="content">
<div class="title">Laravel 5</div>
<div class="quote">{{ Inspiring::quote() }}</div>
</div>
</div>
{!! get_footer() !!}
Your wordpress site should be accessible at www.urdomain.com and laravel app under url www.urdomain.com/app/appointment or adjust you .htaccess if you wish a different URL pattern. Hope this help.
The Document folders are structured like this
/var/wwww/ (root)
java/
...
php/
projectone/
controller/
view/
index.php
...
projecttwo/
...
I want all requests to /php/projectone or it's subfolders to redirect to /php/projectone/view/index.php, while requests to /php/projecttwo and other folders should not be redirected.
I believe using mod_rewrite is the way, however, I still haven't achieved what I want.
Place an .htaccess inside project one folder with the following rule:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /php/projectone/
RewriteRule ^(view/)?index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . view/index.php [L]
And you can repeat that for any other project folders you have. by changing the RewriteBase.
Let's say we have the following schema:
root/
application/
-public/
index.php
css/
img/
javascript/
...
system/
...
I was thinking that if I create an .htaccess file in the root with the following rules:
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ application/public/index.php?url=$1 [QSA,L]
I would be able to make other folder except public unable to be accessed. Assume index.php is the file that load all the file the application need and include and require them (with relative paths obliviously): the application is actually run in index.php and every public file such as css stylesheet, images or javascript script are in the public folder. So you, as user, cannot access the system folder because if you digit www.site.com/ it refers to root/application/public/. Am I right?
But the htaccess rule I set there does not work... If I put that htaccess in the root folder and I try to access the root folder the browser shows me the list of the file of the root folder (application and system). Why isn't it working?
I used this while ago when index.php was in the root folder:
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
and it worked just fine. Why isn't the new one working?
The document root of you project should be set to the /public folder.
This way that's the only folder to web-user would have access to.
Edit: for more info check the apache manual http://httpd.apache.org/docs/2.0/mod/core.html#documentroot
My initial code was right. Except for the third line which obliviously considered / a folder and made me see it instead of redirecting to application/public/.
Commenting the third line everything worked fine.
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ application/public/index.php?url=$1 [QSA,L]
I have such situation.
I'm developing application in Zend Framework and htaccess pointing every request to
index.php. If some file or directory exists on the request path then htaccess allow access to this files like css, js, images etc...
Now I have for example such link:
example.com/profile/martin-slicker-i231
With Zend Router it points to controller account and action viewprofile. I want to add some avatart for my users and I created directory in public folder (so the images would be accessible by the server like css and js). The directory is named "profile" and subdirectory in it is "martin-slicker-i231". All path is visible by server like that:
public/
.htaccess
index.php
profile/
martin-slicker-i231/
avatar-small.jpg
The problem is when i point browser to example.com/profile/martin-slicker-i231 it
points me to this directory not the controller and action. When I remove the folder with user then the flow go to the controller and action. How to configure .htaccess so the example.com/profile/martin-slicker-i231 will be pointing to controller and action but request to example.com/profile/martin-slicker-i231/avatar-small.jpg point to the file. Here is my .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Can somebody help?
Just remove the portion that says that it should serve directories:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Now it will serve files (with size > 0) and symbolic links directly, but send directory references and other urls to your application
According to the Apache docs the following will turn off directory listings
<Directory /path/to/directory>
Options -Indexes
</Directory>
You might also want to look at the documentation for mod_autoindex