Routing to a directory outside of Laravel 4 - php

I'm sorry if this is a very vague question, however I have found no other solutions on stackoverflow or the Laravel 4 docs.
I have the following directory structure
main / laravel
forums / forum installation
localhost points to main / laravel / public
I want to access the contents of /forums in my browser like localhost/forums however since a /forums does not exist in main / laravel / public it returns 404.
Now, I simply can just move the contents of /forums into laravel's public directory however I want to try to get laravel separate from other non-laravel directories like forum softwares and wiki softwares.
So to recap, localhost returns 404 when accessing /forums because localhost points to main / laravel / public and /forums is located outside of the laravel installation. I would like to have localhost/forums access the contents of ~forums and not search in ~main / laravel / public. If it's more logical putting ~forums in laravel's public folder, then please do explain to me because I've only been using laravel for around two months on one project.

You can use an Alias in Apache to route a specific URL to a specific directory on the filesystem. For example:
server-wide (will apply to all VirtualHosts served by Apache)
Alias /forums /path/to/forums/install
or, for a specific VirtualHost:
<VirtualHost *:80>
DocumentRoot /path/to/laravel/install
ServerName your.domain.com
CustomLog /var/log/apache2/access.log combined
Alias /forums /path/to/forums/install
</VirtualHost>
Then anything incoming to localhost/forums will be served from that directory.

Your web server's public directory has nothing to do with Laravel, it's not Laravel's public directory!
So IMO there is no problem to put other application's files in the public directory.
You can also create a separate directory for Laravel's public stuff and put that in your main public directory in order to keep things cleaner. You can simply change Laravel's public directory in bootstrap/paths.php file.
For example from
'public' => __DIR__.'/../public',
to
'public' => __DIR__.'/../public/laravel',

Related

How to remove 'public' from Laravel URL hosted on child folder?

I have a Laravel 5.3 project on shared hosting located at the following address.
How to remove the public from URL?
Now the same question was asked at Remove public from URL Laravel 5.3 on shared hosting but many answers were asking to move all public content to root domain, but my root domain is already hosting something with index.php in it.
Can you please tell me how to do it? All other information is same as the question shared, i.e default .htaccess file in public as well as home (laravel installation) folder. Second I am in shared hosting, I cant change Vhost or something.
Here is my folder structure inside https://yourdomain.tld/home folder
I think there are 3 possible solutions:
Since you use vhost instead of localhost, the problem might be the DocumentRoot declaration. I use Xampp, so the path and code will be based on that. In my case, the file that creates vhost is located:
C:\Xampp\apache\conf\extra\httpd-vhosts.conf
The vhost code is this:
<VirtualHost *:80>
DocumentRoot "C:/Xampp/htdocs/mydomain/home/public"
ServerName mydomain.tld/home
ErrorLog "logs/mydomain-e1rror.log"
CustomLog "logs/mydomain-access.log" common
</VirtualHost>
BTW, make sure you 127.0.0.1 mydomain.tld in "etc" file if you use a Windows machine.
When you enter www.mydomain.tld it should hit the index.php file that is located in public folder.
Since you use very old version of Laravel, I suppose you download it from liveserver. When we deploy our app, we make some changes in public/index.php file to tell our app where Laravel is. Look for
require __DIR__.'/../vendor/autoload.php'; //
I think this is different in your index.php file. BTW, I don't know if this path is different in version 5.3.
Your folder structure is incorrect. You might try to compare it with freshly installed Laravel.

How can the document root be changed in web server behavior?

I'd like to know if there is a way of changing the relative document root for extra security. I'll try to explain myself through the following example:
/root
/app
/public
Say an www.example.com request to the web server would point to the root folder.
I was wondering if there was a configuration, for instance through an .htaccess file located in said root folder, that would make the server point to the public folder instead, therefore having any remote paths always be relative to said public folder.
In this instance, www.example.com/app would request an app folder inside of public, instead of an app folder inside of root, leaving the latter to be inaccessible from a remote url request.
In the same manner, www.example.com/public would request a public folder inside of our root public folder and so forth.
I've read various topics like this one that mention using a custom .htaccess configuration to achieve something similar, but it requires the manual configuration of the request url in said file, while my intention is for it to work without further configuration no matter where you host the application.
Another possible solution I've seen is doing a hard redirect through the .htaccess file, which does not solve anything actually.
Feel free to edit this post as I might have had a hard time trying to get my point across.
You can use this simple .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
Any request on your server will point to the public folder.
Inside the public folder you can add an extra .htaccess file handling your site rules.
Also you can Host multiple sites on One webserver. You can combine making VirtualHosts and Alias using mod_alias mentioned before
In this example is suposed to have your own server (either dedicated or VPS)
By using Virtualhosts you can tell to the webserver when you recieve a request to www.example.com to serve content from a specific folder.
An example Virtualhost of it is:
<Virtualhost *:80>
ServerName ^domain_or_ip^
DocumentRoot ^path of the public folder^
DirectoryIndex index.php home.php index.html index.htm
ErrorLog ^path for a file containing php errors^
CustomLog ^path for logging whitch browser and ip visited your site^ combined
</Virtualhost>
I suggest that you point your server to the public folder anyhow, as it is much more secure, you could see that all frameworks behave the same way, they all have a "public" folder where the server points to.
In the public folder you have one point of entry to your scripts, like
index.php
and from this entry you will communicate with your application.
Of course you can still work the way you requested, and it will work great, but who knows maybe you will miss something and someone could access and view your "inner" files.
You're on Apache web server? If I understand correctly, you're looking for Virtual Directories. Usually, we have to put our web application inside the document root of Apache in order to make the application accessible from the network. However, there is a trick to make the web application still accessible even though we put it outside of Apache's document root. Please read up on that here: http://w3shaman.com/article/creating-virtual-directory-apache
Credits to W3Shaman.com, obviously.

Combine Laravel 5 with non-laravel pages

I am building an application in Laravel which will be in construction for some time. In the meantime, I have 2 "conventional" websites, one static html and one php, which I'd like to include in my source control and make publicly accessible as I build the laravel application.
I have my public folder set up like this :
public/website1/many folders and files
public/website2/many folders and files
public/index.php
I would like to route / redirect users in the following way (lets use my development environment "localhost" as the domain):
localhost/laravelapp/ -> index.php
localhost/website1/ -> website1/index.html
localhost/website2/ -> website2/index.php
This way I can maintain all my code within a single project / source control / server.
But how do I route this!?
I have tried:
Route::get('/website1', function() {
return File::get(public_path() . '/website1/index.html');
});
but this just returns a static file with no relative links to the css folder or other linked files. How can I redirect the user properly to the correct area? Rebuilding the existing sites in Laravel framework is not an option. Thanks.
This should be pretty simple. I assume that you are using apache?
If you put a separate .htaccess file inside public/website1 and public/website2 then those rules will apply instead when you are visiting those routes, ignoring the rewrite rules in public/.htaccess.
You can configure your new .htaccess-files however you want, but all you need is
<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
This is rerouted on the webserver level. Nothing is passed through laravel. This is what you want.
Now, we have solved this part:
localhost/website1/ -> website1/index.html
localhost/website2/ -> website2/index.php
But the laravel app acts like this:
localhost/ -> index.php
instad of:
localhost/laravelapp/ -> index.php
To solve this part, create a new directory inside public called laravelapp
and move public/.htaccess to public\laravelapp\.htaccess and change the following line
RewriteRule ^ index.php [L]
to
RewriteRule ^ ../index.php [L]
This was solved by by doing a couple of things:
1) VirtualHosts on the webserver to handle the domain name routing. Create a file on the Apache server:
httpd/conf.d/vhost.conf
NameVirtualHost *:80
<VirtualHost *>
ServerName domain1.com
ServerAlias dev.domain1.com
DocumentRoot /var/www/html/public
</VirtualHost>
<VirtualHost *>
ServerName domain2.com
ServerAlias dev.domain2.com
DocumentRoot /var/www/html/public/website2
</VirtualHost>
This routed my domains to their subfolders
2) The localhost environment was not working well. I switched from using "php -S server.php" (Laravels own server script configuration) to MAMP for OSX. This instantly cleared up my remaining permissions issues.
I hope this answer can help someone else in the future.

using laravel in a subdirectory

It seems to me that the idea behind laravel is that the public/ directory is where your DocumentRoot points to and that the app/, vendor/, bootstrap/ and build/ directories all live outside the web root. But what happens if you want the laravel project to live in a subdirectory.
ie. http://www.domain.tld/ might be static HTML, http://www.domain.tld/phpbb/ is phpBB and you wanted your laravel app to live at http://www.domain.tld/app/.
Should you just put the entire laravel folder in http://www.domain.tld/app/ and redirect, via .htaccess, all requests to http://www.domain.tld/app/public/? That seems inelegant.
Maybe you could put laravel in http://www.domain.tld/ and rename the /public/ directory to /app/ but that seems even more inelegant.
Any ideas?
The question is why you need it in subdirectory. So far I never need to put custom project into subdirectory. I use domain or subdomain if needed.
You could try with:
renaming your public directory to the directory name you want to use
put other directories into your root domain directory
add .htaccess for those directories with deny from all (to block access from your root domain)
edit paths in bootstrap/paths.php
I haven't tested it and I wouldn't probably even try to do that. Many frameworks are rather created to use them on separate domains or subdomains and not in directories so I would recommend you to rethink it.
Use virtual hosts in your web engine (eg. apache, nginx, IIS, etc.) and point the document root for your alias to your laravel app.

Storing multiple PHP apps on one domain

I have got myself a server, and on it I'd like to host my portfolio. My portfolio will be built using a tiny PHP MVC framework I wrote myself. This will be accessed by going to domain.com (for instance).
Now, on my portfolio, I want to host all of the other pieces of work I do. These will also be PHP MVCs. So, for example, domain.com/app1, domain.com/app2 and so on. However, as you probably know, the nature of a PHP MVC means that some files are not meant not be in the DocumentRoot (talking about Apache here). So, if you wanted to store all of your work like this, how would you go about achieving it?
An example directory structure of one of my apps:
/application
/views
/models
/controllers
/public
/js
/css
Therefore domain.com/css should point to that apps css folder which is in the public folder, as should domain.com/app1/css point to its css folder in its public folder.
I'm just trying to work out a good way of organising my work. I would really appreciate anyones thoughts!
I would suggest that you use an apache alias for each new application :
http://httpd.apache.org/docs/2.2/mod/mod_alias.html#alias
You will keep only one Virtual Host :
<VirtualHost *:80>
ServerName domain.com
Alias /app1/ /var/www/app1/public/
Alias /app2/ /var/www/app2/public/
</VirtualHost>
Well
I would have my folders something like this:
/lib (main portfolio lib)
/projects
/project1 (main lib for project 1)
/public_html (main portfolio public root)
/projects
/project1 (main public root for project 1)
The only thing here is that for project 1, you'd have to set the lib folder to
../../../lib/projects/project1
as opposed to it's standalone
../lib
I hope that helps

Categories