I have set up an Apache server with the following URL: https://dev.mysite.com. I am now creating subdirectories for each project that I have in development. For example it might be https://dev.mysite.com/project, or https://dev.mysite.com/anotherproject. Some of the projects in development utilize the Laravel, and the Laravel Lumen, framework(s).
I will not be using a vhost file for each project, as they will be existing in subdirectories. The goal is to be able to visit https://dev.mysite.com/project/public/ and have the Laravel, or Lumen, application served to the user visiting the project. I am ok with having the client visit /public/ to view their project. This is not happening.
-Indexes has been set on the server's virtualhost file to prevent listing files/directories in the browser.
When I navigate to {url}/project/public/heartbeat (for example) the route is not being served (or parsed) correctly. When I do a die of the $_SERVER['REQUEST_URI'] I get /project/public/heartbeat. The route heartbeat is set up as follows: $app->get('/heartbeat', 'Controller#method');. Visiting this url gives the typical HTTP not found exception from the Illuminate code. However; when I add project/public/ to the beginning of the route path (resulting in $app->get('/project/public/heartbeat'...) it works. I have tried adding the RewriteBase / and RewriteBase /project/public/ and RewriteBase /project/ settings to the .htaccess file and neither work. I do know that the .htaccess is being used (I can add 'asdf' to the file and break it). The mod_rewrite module is enabled.
Here are the configuration files for the vhost, and the project/public/.htaccess file.
.htaccess:
<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]
</IfModule>
vhost:
<VirtualHost *:80>
ServerName dev.mysite.com
Redirect permanent / https://dev.mysite.com
</VirtualHost>
<VirtualHost *:443>
ServerName dev.mysite.com
DocumentRoot /var/www/public
<Directory /var/www/public>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/dev-error.log
CustomLog ${APACHE_LOG_DIR}/dev-access.log combined
ErrorDocument 403 https://dev.mysite.com
</VirtualHost>
Please let me know if you need any further information. Again, end goal is to be able to visit https://dev.mysite.com/project/public/ and have the Laravel, Lumen, etc. application served to the user visiting the project.
Setup your vhost as follows:
<VirtualHost *:80>
DocumentRoot "/some/directory/mysite.dev"
ServerName mysite.dev
</VirtualHost>
Notice you need to setup an initial 'root' project at /some/directory/mysite.dev. This doesn't have to be a laravel app, all it needs to have is a .htaccess file, and maybe you might want an index.html file in there so when you visit mysite.dev, you can at least see something. This is optional though.
In the /some/directory/mysite.dev folder, create a .htaccess file and add the following:
<IfModule mod_rewrite.c>
RewriteEngine On
# We need this to stop the rewrite rule looping as the URI
# and directory both start with 'SubProject'
RewriteCond %{REQUEST_URI} !^/SubProject/
# direct all requests that start with SubProject
RewriteRule ^ SubProject/?(.*)/?$ /SubProject/public/index.php [L]
</IfModule>
When you visit mysite.dev/SubProject the index.php file from that project should be loaded.
Related
Currently trying to set up a Laravel project on my local PC.I added the hostname 127.0.0.1 localhost-smt to the Windows hostfile and edited my Apache httpd-vhost.conf:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/xampp/htdocs/project1"
<Directory "C:/xampp/htdocs/project1/">
DirectoryIndex index.php
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName localhost-smt
DocumentRoot "C:/xampp/htdocs/project1/smt/public"
ErrorLog "logs/testseite-error.log"
CustomLog "logs/testseite-access.log" common
<Directory "C:/xampp/htdocs/project1/smt/">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
My Apache httpd.conf
DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/htdocs">
Each time I connect to localhost-smt/home I am receiving a double URL like http://localhost-smt/127.0.0.1/home. Further more if I navigate from localhost to the public folder and add /home i receive the following URL http://localhost/smt/public/127.0.0.1/smt/public/home.
I tried a lot of different solutions, but none of them worked so far. I would appreciate if someone could help me solve this riddle.
I'm not sure how to do this using .conf files but the way I used to achieve this when I used Xampp was as follows, doing it this way will obviously mean undoing anything you have done to the default behaviour of Xampp relating to the .conf files you have edited.
Add the name resolution to your hosts file
127.0.0.1 dev.projectName
Add the following .htaccess file into the root of your projects directory (typically "C:/xampp/htdocs")
RewriteEngine On
RewriteCond %{HTTP_HOST} !^dev.projectName$
RewriteRule .? - [S=1]
RewriteRule ^(.*)$ projectName/$1 [L]
add the following .htaccess file to the root of your project (typically "C:/xampp/htdocs/projectName")
RewriteEngine On
RewriteBase /dev.projectName/
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
Add the following .htaccess file into your public directory (typically "C:/xampp/htdocs/projectName/public")
RewriteEngine On
RewriteBase /dev.projectName/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
this can be viewed by going to http:\\dev.projectName
I know it is a long-winded way of doing it but it was the only solution I could find at the time and it worked for me.
It does become a ball-ache having to do this every time you create a new project which is one of the reasons I switched to docker which in my opinion is a much better solution anyway,
LARAVEL 5.0 | APACHE 2.4.7 | PHP 5.5.8
I know this is a pretty popular question. A lot of people saying to move public contents to root directory (which i find awkward because of security issues), rewriting the .htaccess, creating virtual host.
I tried creating virtual host because i think this is more acceptable solution.
but i'm encountering some problems with my configurations:
I'm running easyphp for the meantime, id like to test if i can get rid of the public route in url before setting this up live.
httpd.conf
NameVirtualhost *:80
<VirtualHost *:80>
DocumentRoot "${path}/data/localweb/quantum.dev/public"
ServerAdmin admin#localhost
ServerName quantum.dev
ServerAlias www.quantum.dev
<Directory "${path}/data/localweb/quantum.dev/public">
AllowOverride All
Options Indexes FollowSymLinks
Order deny,allow
Allow from quantum.dev
Deny from all
Require all granted
</Directory>
</VirtualHost>
I modified C:\Windows\System32\drivers\etc
hosts
127.0.0.1 quantum.dev
When I try to browse http://127.0.0.1/quantum.dev/ it will load the listof files and not the project inside public
http://127.0.0.1/quantum.dev/
Please revert all your changes to the directory. restore the default structure and configuration of the project. If i'm not mistaken and correct me if i am, you are using a shared hosting provider. If that is the case, here is a more proper way of doing so.
In your .htaccess, you need to add the following.
At the top of the file add this line:
DirectoryIndex public/index.php
and in here:
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Change the last rule to this:
RewriteRule ^ public/index.php [L]
Now, all you need to do is to move this, and only this(the .htaccess file), to the root folder of your project. In this way, you are maintaining the laravel project structure in its original state.
put this .htaccess file near public folder
Options -MultiViews
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]
When you are selecting a directory for your domain in c-panel, simply provide path till the Laravel's public directory and not just till the Laravel's root directory. That way you don't need to change .htaccess at all.
I have just recently upload my site to the production and follow above steps. It worked very well.
This is my configuration that is working fine for me. So first of all, your Virtual host configuration should be like that :
ServerAdmin webmaster#domaine.dev
ServerName domaine.dev
DocumentRoot /var/www/mysiteweb/public/
<Directory /var/www/mysiteweb/>
Options -Indexes
DirectoryIndex index.php index.html
AllowOverride All
</Directory>
Anf after, make this on your .htaccess laravel file :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Don't forget to check if the mod_rewrite is enable.
Hope that helped. Regards.
I recently bought a VPS and I'm trying to deploy there a personal laravel project which runs perfectly in localhost.
What I've done is to tar all the project and decompress it on the VPS (/var/www/). I've installed a LAMP, and create the site configuration (/etc/apache2/sites-available/anuncios.com.conf) and enabled it (creating the link on /etc/apache2/sites-available) like so:
<VirtualHost *:80>
ServerName 137.94.162.235
DocumentRoot "/var/www/anuncios.com/public/index.php"
<Directory "/var/www/anuncios.com/public">
AllowOverride all
</Directory>
</VirtualHost>
It's a laravel 5.1.26, with the tipycal .htaccess under the public dir:
<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]
</IfModule>
The site works but it's not loading the resources like .js or .css files. Checking the link, for instance:
http://137.94.162.235/css/externos/bootstrap.min.css
I got a Laravel error: 404 Uups, page not found.
Any help for making it work please?
Ownership is 1000:apache2, and permissions are the same that in my localhost (except public that now is 777 just to be sure).
Edit: I followed Huzaib's advise and now it loads everithing, the remaining problem is that now I have to address the site as 137.94.162.235/index.php otherwise 137.94.162.235 it's not working.
In your apache configuration, DocumentRoot has to be a directory not a file.
DocumentRoot "/var/www/anuncios.com/public/"
Make the advised change and then restart the apache2 service.
Solved,
Firstly, I followed Huzaib Shafi answer and changed the /etc/apache2/sites-available/anuncios.com.conf changing the DocumentRoot to the public folder (not the index.php).
Finally, to avoid having to specify the index.php I had to enable the apache mod rewrite from the command line:
a2enmod rewrite
That allows apache to follow some instructions from the public/.htaccess.
Let's suppose I have a website named
foo.com
I can access foo.com and it runs the index.php found in the root folder. My question is: how should I edit the vhost file to enable rewrite mod without enabling htaccess?
My goal is to be able to write
http://foo.com/bar/loremipsum/dolor
into my browser address bar and to run index.php regardless of the number of / characters in the url. My index.php would handle the parameters separated by /
How can I achieve this?
EDIT:
vhost file:
<VirtualHost *:80>
ServerName myproject.com
ServerAlias www.myproject.com
ServerAdmin webmaster#localhost
DocumentRoot /opt/apps/myproject
<Directory /opt/apps/myproject>
# disable htaccess
AllowOverride None
# route everything to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EDIT: The problem, as the accepted answer suggests is that this host did not contain the line which turns the rewrite engine on. This line is present in the answer. Which solves the problem.
To disallow the use of htaccess, you need this directive in a <Directory> container for your document root, then you can just place mod_rewrite rules in the same container:
<Directory "/var/www/htdocs/">
# disable htaccess
AllowOverride None
# route everything to index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
</Directory>
assuming that "/var/www/htdocs" is you document root.
To ensure mod_rewrite is loaded, check the httpd.conf file for a LoadModule line that contains mod_rewrite, and make sure it's uncommented. You'll need to restart your server anytime you make changes to the vhost config.
Short explanation:
The lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
are conditions to check that the request is not an existing file (-f) or an existing directory (-d). These conditions serve 2 main purposes:
It prevents the rewrite engine from looping, so that index.php won't also get rewritten. Since index.php is an existing file, the conditions stop the rewrite engine.
It allows resources and assets like images or scripts from being rewritten.
If you want everything routed to index.php no matter what (including images or anything else), then you can change the 2 conditions to simply:
RewriteCond %{REQUEST_URI} !^/index.php
so that everything gets rewritten except index.php.
Symfony uses the following typical .htaccess file:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
and I have this as my vhost:
<VirtualHost 127.0.0.1:80>
ServerName jobeet.loc
DocumentRoot "C:/wamp/www/jobeet/web"
DirectoryIndex index.php
<Directory "C:/wamp/www/jobeet/web">
AllowOverride All
Allow from All
</Directory>
Alias /sf "C:/wamp/lib/symfony-1.4.1/data/web/sf"
<Directory "C:/wamp/lib/symfony-1.4.1/data/web/sf">
AllowOverride All
Allow from All
</Directory>
The above works perfectly, but I want to know how to map and additional debugging url, http://jobeet.dev to automatically serve the frontend_dev.php file so I can use urls like:
http://jobeet.dev/jobs/...
instead of
http://jobeet.dev/frontend_dev.php/jobs/...
to map to the debug .php file in the framework.
I tried adding a duplicate of the vhost entry and simply changing the servername and directoryindex to
ServerName jobeet.dev
DirectoryIndex frontend_dev.php
but understandably this does not work, as I believe I would need to check the URL in the .htaccess to do this?
Can anyone offer some advice regarding this?
Thanks in advance!
:)
First add jobeet.dev as a ServerAlias in your current VirtualHost so it can share the same hosting configuration:
<VirtualHost 127.0.0.1:80>
ServerName jobeet.loc
ServerAlias jobeet.dev
DocumentRoot "C:/wamp/www/jobeet/web"
....
Don't forget to restart Apache when you're done.
Next, turn on no_script_name in your dev configuration in apps/frontend/config/settings.yml:
dev:
.settings:
no_script_name: true
Now your dev web controller (frontend_dev.php) won't show up in your auto-generated URLs (from link_to(), url_for(), etc).
Finally, set up a RewriteRule for your dev domain before your production controller comes in to play to route everything coming in at jobeet.dev to your dev web controller:
RewriteEngine on
...
...
RewriteCond %{HOST_NAME} ^jobeet\.dev$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ frontend_dev.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
That should do it.
I'd recommend you to map all needed hosts (jobeet.loc, jobeet.dev, etc) to SF_DIR/web, set index.php as dir-index (as you did) and in that file just run particular app with a particular env depending on $_SERVER['HTTP_HOST'].
Hope I described good to make an idea clear.