Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache rewrite for Laravel /public

I am sorry about this, but my htdocs root is wrong and I can't change that. So I have to make it work in the /public folder.

I use the normal Laravel .htaccess file with the following rewrite:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

If I open http://kemtime2_neu.pr.domain.de/public I get redirected to http://kemtime2_neu.pr.domain.de/public/http://kemtime2_neu.pr.domain.de/public/login

How can I fix this?

I would love to make it work from http://kemtime2_neu.pr.domain.de/ but getting it to work with http://kemtime2_neu.pr.domain.de/public/ would be fine.

like image 883
PiTheNumber Avatar asked Dec 15 '22 12:12

PiTheNumber


1 Answers

I use the 3 solution of this post and works fine:

http://web.archive.org/web/20130320184846/http://forums.laravel.io/viewtopic.php?id=1258

Solution 1 - Alternate installation path with symlink.

This is the preferred solution and in general an all-around good idea. It's possible to install your application to a folder unrelated to public_html/ and then symlink the public folder to the public_html/ path.

For example:

Install your application to /home/applications/mysite.com

Imagine that your DocumentRoot points to /var/www/vhosts/mysite.com/httpdocs

Remove the httpdocs folder from the mysite.com vhosts folder then connect the two with a symlink: ln -s /home/applications/mysite.com/public /var/www/vhosts/mysite.com/httpdocs

Solution 2 - .htaccess with mod_rewrite

This solution enables you to drop Laravel into your public folder then use a .htaccess file to redirect requests to the public folder. This solution places your application and core system code into a publicly accessible folder. This is not something that we encourage you to do with any PHP framework.

Step 1. Place Laravel in your document root folder.

Step 2. Place the following .htaccess file in your document root folder.

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Step 3. Make sure that you manually set your 'url' configuration in application/config/application.php otherwise Laravel will generate incorrect URLs. Make sure that each of your environments have the correct application.url configuration. For more information on environment-specific configurations see: http://laravel.com/docs/install#environments

Solution 3 - merge the public folder into the installation root

This solution places your application and core system code into a publicly accessible folder. This is not something that we encourage you to do with any PHP framework.

Copy the contents of the public/ folder into your Laravel installation folder then change this line in your index.php file from:

require '../paths.php';

to

require 'paths.php';

Keep in mind that any bundles, libraries, or other types of third-party code may not be designed to be publicly accessible.

Note: It's also important to note that your bundles/ and public/bundles/ directories will now conflict. When using this approach you may want to not use artisan's bundle:publish task without knowing exactly what your bundles want to publish.

like image 53
Christian Avatar answered Jan 04 '23 07:01

Christian