How to use Laravel app in subfolder of Apache User Directory - php

I'm trying to get a Laravel 4 app installed in a subfolder of the web root directory for one of my users. I know there are major security concerns having the entire framework accessible via the web. I plan to address those separately after I understand how to get this working.
I've tried every combination of Apache Aliases, and Rewrite rules I can think of, but nothing seems to work. Here's my directory structure:
/home/username/public_html
.htaccess
my_laravel_app/
app/
bootstrap/
public/
.htaccess
index.php
robots.txt
...etc (standard Laravel 4 public folder contents)
vendor/
...etc (standard Laravel 4 files and folders)
The first .htaccess file, the one listed directly under the users public_html directory, contains the following:
AuthType Basic
AuthName Private
Authuserfile /path/to/.htpasswd
Require valid-user
The second .htaccess is a standard Laravel 4 .htaccess file. It contains the following:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I would like to essentially treat this my_laravel_app/public directory as a webroot, so that the Laravel app functions the same way it would if for a normal Laravel installation directly on a domain.
I believe I just need to edit the first .htaccess file to contain rewrite rules that send any request to /my_laravel_app to my_laravel_app/public, but I can't seem to get this working. Can anyone help?

This may be different depending on your setup, but the base config is:
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/my_laravel_app/public/%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/my_laravel_app/public/%{REQUEST_URI} -f
RewriteRule . public/%{REQUEST_URI} [L]
RewriteRule . public/index.php [L]
And put inside your my_laravel_app as an .htaccess to limit the complexity of the conditions. You get the idea hopefully.

Related

Rewriting the URL's of 2 subfolders in a /public (root document) folder

Currently I have a "public" folder being root directory where all my frontend code goes.
But since my public folder contains both the website content and app content, I would like to split it more up.
So my idea was:
.htaccess (rewriting public url)
src (php backend code)
public/
web/
index.php
app/
account.php
So my problem is, I can easily go to the pages like this:
http://example.com/web/index.php
http://example.com/app/account.php
but I would like to remove "web" and "app" from the URL's...
http://example.com/index.php
http://example.com/account.php
How can I do that using .htaccess? I'm running apache 2.4.
EDIT
An example of what I've tried:
RewriteEngine On
RewriteCond %{REQUEST_URI} !app/
RewriteRule (.*) /app/$1
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1
Probably you need to tune up your requests. But a first approach is:
First, make sure that you have mod rewrite enabled.
Second, write the following in the .htaccess
RewriteEngine On
RewriteRule ^[/]?index.php$ /web/index.php [NS,NC,L]
RewriteRule ^[/]?account.php$ /app/account.php [NS,NC,L]
You will need to read the documentation to make other rules. You can read it here
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
Also pay special attention on the flags. Read the documentation to set the ones that applies to your situation.
I've tried so many different things now, but I finally figured it out.
RewriteEngine on
RewriteBase /
# Rewrite every web request
RewriteCond "%{DOCUMENT_ROOT}/web/%{REQUEST_URI}" -f
RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/web/$1"
# Rewrite every app request
RewriteCond "%{DOCUMENT_ROOT}/app/%{REQUEST_URI}" -f
RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/app/$1"
# Remove .php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This works when having a setup:
public/ (root folder)<br>
web/ (contains website frontend)<br>
app/ (contains app frontend)<br>
.htaccess (contains the code above)

Error writing .htaccess to protect Symfony Directories in Web App

I'm trying to publish my first Symfony Project in production, but I'm very worried about security, I have taken the following measures:
1) First: Delete on /var these directories: cache, logs and sessions
2) Second: Changed of default name for cookie session (client side: symfony)
3) Trhee: Creating a .htaccess in root directory of application
My problem is about the .htaccess. The goal is dennied access to others directories of application like: /app, /src, /test, /var, /vendor.
I'll write the follow rules in .htaccess and puted it in root directory of my app: myDevelopedApp/.htaccess.
.htaccess
#Checking if module is avalaible
<IfModule mod_rewrite.c>
#Using the rewrite engine
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#Rewrite rule to redirect the request
RewriteRule ^(.*)$ web/$1 [QSA,L]
<IfModule>
The problem it's not working, I can not access to web folder when I write the rules. Apache hides the application myDevelopedApp when I try to access it by browser
What I'm wrong?
Any other recommendation to improve security in production enviroment is welcome, thanks so much.
If your are really concerned about security you should follow the Symfony best practices. That means that only the /web folder should be accessible in the vhost.
Moreover if you initialized your project with a standard edition you should already have .htaccess files in the app/ src/ folders.
htaccess file code
.htaccess
#Checking if module is avalaible
#Using the rewrite engine
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#Rewrite rule to redirect the request
RewriteRule ^(.*)$ web/$1 [QSA,L]
and this code too on your .htaccess file
<pre>
.htaccess
#Checking if module is avalaible
<IfModule mod_rewrite.c>
#Using the rewrite engine
RewriteEngine On
RewriteBase /folder/ /*location address if localhost localhost/project */
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#Rewrite rule to redirect the request
RewriteRule ^(.*)$ web/$1 [QSA,L]
<IfModule>
</pre>
RewriteBase solve this problem

laravel publish site issue

I need to publish my web app written in laravel 4,
i've followed many tutorial in internet but i'm not able to make it works correctly.
i'm on a dedicate server with centos 6.6 where resides another website so the laravel app should be placed in a sub folder named vols
this is the current folder structure:
/
var/
www/
laravel/
app/
bootstrap/
vendor/
...
html/
vols/
other files of my website
i've changed the paths inside bootstrap/paths.php to
'app' => __DIR__.'/../../laravel/app'
'public' => __DIR__.'/../../html/vols'
'base' => __DIR__.'/../../laravel'
'storage' => __DIR__.'/../../laravel/app/storage'
and the paths inside vols/index.php to
require __DIR__.'/../../laravel/bootstrap/autoload.php';
$app = require_once __DIR__.'/../../laravel/bootstrap/start.php';
my first controller is named volunteers, so if visit http://mydomain/vols i should be redirected to http://mydomain/vols/volunteers
instead i see this error message: The requested URL /vols/volunteers was not found on this server.
if i try to connect to http://mydomain/vols/index.php/volunteers everything works correctly but i don't want index.php in my route.
i think i should work on .htaccess inside vols folder, but i've never studied how it really works, so i'm just copying some random snippets from internet substituting public with vols.
currently it look like this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^vols
RewriteRule ^(.*)$ vols/$1 [L]
</IfModule>
Some solutions suggests to create vhost, technically i can do that, but i don't know how.
Could you please help me?
You should place the following .htaccess in your vols folder -
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /vols/
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

.htaccess affects directory in the sub domains

Consider this is my domain www.example.com
I am using laravel in my website
This is the laravel structure
app/
bootstrap/
public/
vendor/
server.php
To remove the index.php and public from the url i followed this answer
i.e.,
had the .htaccess in the root path
and renamed the server.php file as index.php
Here is my .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
It works good, but the problem is i am having subdomain as
projects.example.com
While i create a sub directory here i.e.,
projects.example.com/firstproject
It always shows the internal server error
How can i fix this like, having exception to that directory or something like that ?
The htaccess file affects the directory the file is in and all subdirectories. If you have subdirectories that you don't want mod_rewrite rules to affect, then you need to add an htaccess file with in the subdirectory with the rewrite engine turned on (so that none of the parent rules have precedence).
Just add this to an htaccess file in your subdirectory without any actual rules:
RewriteEngine On

Redirect all requests to a directory and it's subdirectories to a file inside the directory, using Apache2 server

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.

Categories