How to deploy Symfony2 on a shared-hosting? - .htaccess

I want to access my symfony app in production env (http://www.sample.com/amateur1/web/app.php) from this url http://www.sample.com/amateur1.
To do that I moved the .htacces file to http://www.sample.com/amateur1/.htaccess with this contents:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
But when I go to http://www.sample.com/amateur1 shows a 404 Error, and prod.log isn't written.
I also used RewriteBase /amateur1/web/ because I don't know If RewriteBase path is relative to the DocumentRoot of the server, Or from the path where the .htaccess file is located. Also tried /amateur1 and /amateur1/ as RewriteBase due to this answer Symfony2: How to deploy in subdirectory (Apache)
With the three try's, The Exceptions page appears unstyled, and not loading any image. But then I get the following error in prod.log file:
[2013-02-15 02:06:47] request.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /amateur1/" (uncaught exception) at /home/u105859802/public_html/amateur1/app/cache/prod/classes.php line 5121 [] []
What I'm doing wrong ?

In your configuration, apache uses public_html as the document root
If Symfony2 is installed in directory /home/u105859802/public_html/amateur1, the Symfony public directory to serve is /home/u105859802/public_html/amateur1/web/
You should use
RewriteBase /amateur1/web/
But beware, it is not safe
You have to protect your symfony directories! (configuration is accessible)
Why don't you try moving your symfony files in your private area ?
You can rename the Symfony web directory to public_html
See how to do that in documentation cookbook
So, my recommendation structure looks like below :
/home/u105859802/
vendor
src
app
bin
public_html (web by default in symfony2) <- the only public directory

In most of the shared hosting, you can't override Apache settings in that case may need to wright a redirection rule in .htaccess file
Options +FollowSymLinks +ExecCGI
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule !^projectName/ /projectName/web/app.php/%{REQUEST_URI} [L,NC]
this is the rule I used in my project .
You may need to provide full paths to your assets other ways they will not get loaded .

Related

Symfony in subfolder of DocumentRoot doesn't include RewriteBase

I have a weird situation with Symfony 3.2.6. The whole code is above public_html directory which is a DocumentRoot.
In public_html I have a directory called panel. In this directory I have put app.php and app_dev.php controllers with .htaccess. I have setup properly require and include_once paths.
The problem is that when I run mydomain.com/panel/app_dev.php I get an error:
No route found for "GET /panel/app_dev.php"
So it seams that this code in .htaccess doesn't work properly for this situation.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
So I have commented out this and used this after RewriteEngine On
RewriteBase /panel/
but this gives the same result. Anyone knows how to fix it?

Laravel project in subfolder of public_html gives trouble with .htaccess redirect

I've set up my VPS server and am trying to set up a Laravel project.
I have a /public_html/ folder with an index.html that's shown when visiting the website URL.
I have a laravel project in: /public_html/project_one/
The original index.php is located in /public_html/project_one/public/index.php
I want the website to show to Laravel project_one by default, but since yesterday I literally googled this for 4 hours, I tried every solution on stackoverflow, nothing worked... I think I've tried over 10 versions of .htaccess I could find.
The question:
Does anyone know how to setup the .htaccess inside /public_html/ so it redirects to my Laravel project?
I though I needed to upload my Laravel folder in: /public_html/project_one/ but is there a better location?
I want the main url to show the Laravel project. So www.mainurl.com is Laravel's project_one/public/index.php file. I do not want /project_one/ to be written in the URL!
Just out of curiosity, if I now visit: www.mainurl.com/project_one I get a 403 forbidden.
Failed solutions
In /public_html/ I tried to add .htaccess with:
DirectoryIndex index.php
#Redirect to /project_one/public if you haven't already and the IP is okay
RewriteCond %{REMOTE_ADDR} ^1\.2\.3\.4*
RewriteCond %{REQUEST_URI} !^/project_one/public
RewriteRule ^(/lucaphoto)?(.+) /project_one/public$2 [L]
#if IP does not match and you ARE in the folder, then Redirect to root
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4*
RewriteCond %{REQUEST_URI} ^/project_one/public
RewriteRule .? / [R=301,L]
This gives me something really strange:
On Chrome: it redirects me to www.mainurl.com/project_one -> And then gives me a 403 forbidden error.**
On safari: it redirects me nowhere but gives me this error:
Forbidden. You don't have permission to access / on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
Laravel's original .htaccess File
By the way, Laravel has an original .htaccess file at /project_one/public/.htaccess. And it looks like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
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>
The actual laravel application ( anything but the public folder ) is not supposed to be public and therefore should not be placed inside the public ( public_html ) folder.
I actually tried severel things to get something similar to your setup running. My conclusion was - that only changing the .htaccess file will net e enough to run laravel inside a public folder.
The most easy solution to quickly setup a laravel application is to use a subdomain which directly points to the public folder.
www.mainurl.com /* points to .../public_html/ */
laravel.mainurl.com /* points to .../laravel/public/ OR ( but not good ) */
/* points to .../public_html/my-project/public/ */
What you actually will need to do then is to change the root of your domain / subdomain to point into another folder - no changes to .htaccess are needed then.
Update
If your site is www.example.com and you want your laravel app to show when opening this site you have two options.
Put the content of laravels public folder to public html and change the references to fit the public_html instead of public folder.
If your Filestructure on the server is like .../var/stuff/.../public_html/ then upload everything to ../var/stuff/my-project and then go to your hosters interface and make an internal redirect to the ../var/stuff/my-project/public folder and everything will be fine without chaning anything in your laravel code. ( anything but .env )
So the actual problem is that your domain points to the folder public_html, instead of the public folder of your app. However please dont put your app into public_html directly, put it parallell to it and change where the domain points to.
if you give us some information about your hoster we might can provide a guide

Symfony2 in a subdirectory

I am trying to configure a Symfony 2.3.3 project into a subdirectory named dev to access it like so :
www.mydomain.com/dev/
Thing is, it is correctly calling the front controller but I get the following error :
NotFoundHttpException: No route found for "GET /dev/"
Well, I don't like the idea to change all my routes in the routing.yml file. So I tried manipulating the .htaccess files with RewriteBase, but I could not found the solution.
Is there some config to change to Symfony in order to get this to work ?
Do I need to change anything in a .htaccess file ?
I use the following web/.htaccess file : Symfony standard web/.htaccess
My root .htaccess just contains this :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/phpmyadmin
RewriteRule ^(.*)$ web/$1 [QSA,L]
</IfModule>

Cake php Sub Folder Installation, Temp folder errors from htaccess or file permissions?

My client has asked for a cake php app i've made for them to go into a sub directory on their existing server.
All works perfectly on my test remote and local installations.
Initially I had a problem with htaccess and had to add RewriteBase /quotesystem/ to my htaccess files in order to see the site at all. So I have changed my htaccess as below:
cake root .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /quotesystem/
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
app .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /quotesystem/app/
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
and webroot .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /quotesystem/app/webroot/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
Now I can access my site but i get loads of warnings about the temp directory:
Warning (2): SplFileInfo::openFile(/web/sites/_p/_e/_t/petersen-stainless.co.uk/public/www/quotesystem/app/tmp/cache/persistent/cake_core_cake_en-gb) [function.SplFileInfo-openFile]: failed to open stream: No such file or directory [CORE/Cake/Cache/Engine/FileEngine.php, line 295]
You can see it here: http://www.petersen-stainless.co.uk/quotesystem
Now, i've CHMOD the temp directory to 777 so everyone should have permissions but i still get these errors.
I've deleted all of the files in these folders so that the temp directories have been cleaned.
When I use my ftp client - transmit - to look at the file permissions the owner appears as (?) and if i try to change it i get an error:
Could not change owner of “tmp”. Server said: 'SITE CHOWN' not understood
I dont get this with any other site I work on.
I don't understand enough to know what is going on, is this a permissions problem or an .htaccess problem or both? Or is it a server configuration problem?
If anyone can help shed some light on this and help my understanding i'd be very grateful.
UPDATE:
My directory structure on the server is as follows:
-- public
--- www
----- quotesystem
------- lib
------- app
--------- config
--------- webroot
In my experience, the SplFileInfo warning appears when Cake attempts to read/write cache that it does not have permission to access. Make sure your application has permission to read/write the cache/tmp folder (depending on your Cake version).
It's probably a .htaccess problem. The RewriteBase should be the same for both the webroot and app .htaccess. So the line in both files should be:
RewriteBase /quotesystem/
I personally never touch the .htaccess in the Cake root directory as it doesn't seem to be necessary to alter anything there.
In addition, /tmp/cache and it's subdirectories should be writable as well.

Moving Codeigniter Project to Amazon EC2, htaccess and ModRewrite Problems

I am attempting to move my Codeigniter projects from a shared hosting service to Amazon EC2 and am running into some challenges with my .htaccess settings. These settings enabled me to not have to include index.php in the url. My previous .htaccess is shown below and worked as expected with my previous hosting provider:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /simple.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
With my Amazon EC2 instance, I now have full access to the LAMP stack and have confirmed that the Apache Mod_Rewrite module is enabled.
I have set up my EC2 instance with two Virtual Host directories, one for each of my sites. Both sites serve up their CI applications as expected, but require index.php to be in the url.
I've tried inserting my Amazon EC2 public path into the .htaccess file as follows but still no luck:
RewriteBase /ec2-55-27-191-14.compute-1.amazonaws.com/simple/
I am sure this is 100% user error on my part and would greatly appreciate your diagnosis!
To answer my own question...
I wound up changing the Apache file /etc/apache/sites-available/default from AllowOverRide = None to AllowOverRide = All.
have you also tried
RewriteBase /
or RewriteBase /simple/
or along those lines?
in my dealings on servers i've used, i've never had to put the domain into the rewrite base. Just the folders

Resources