I've searched through Web to solve my problem but non of the solutions works for me there, so after couple of hours of struggling with Routing module I've decided to ask you for a helping hand.
Problem
I am unable to access Laravel application from outside /public directory. I need to type localhost\projects\laravel\public in my browser but what I want is to use the URL withour /public. The main reason is because I use shared hosting and have no access to apache configuration file so I'm unable to create vhost.
Background
I've installed manually Laravel framework under the: **c:\xampp\htdocs\projects\laravel** and used composer to do the rest for me.
The routing has been set to: Route::get('/', 'HomeController#showWelcome');
In my project root directory **c:\xampp\htdocs\projects\laravel** I've put a .htacces file with the following content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /projects/laravel/
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
In my Application Root dir c:\xampp\htdocs\projects\laravel\public I have .htaccess with the following code in it:
<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>
When I type: localhost\projects\laravel\ I've got NotFoundHttpException
When I type: localhost\projects\laravel\public I've got welcome page
Any help would be appreciate, Thanks.
Finally I've got this thing working. There are two solutions that can be made.
One requires to move all -public content level-up
Second requires slightly changes in your shared hosting directory root structure - if allowed
Solution #1
The first that works for me that I didn't want to implement has been posted by #Wasim in this thread: Laravel 4 removing public from URL The solution is not save as the content core structure is in the same directory as application itself. This could cause some problems in future implementation.
You need to move all the content from public/ folder one level-up into project ROOT directory then replace internal paths in index.php file for correct onece. For security reasons this .htaccess file needs to be put into project ROOT directory
<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>
Modified Laravel structure described above can be uploaded to your shared web hosting and the application should runs withour /public in yor http address.
Solution 2 (with no access to http.conf file on your shared hosting)
This solution does not requires form you to move the content of the /public folder one level-up but requires form you to have read and write access to ../ROOT directory in your shared hosting (../ROOT directory mostly contains public_html, public_ftp and other folders).
You need to move Laravel scructure into ../ROOT directory as follows:
app/
bootstrap/
vendors/
public_html/
public_ftp/
(...)
Files form /public folder goes to public_html
public_html/index.php
public_hmtl/.htaccess
public_html/packages/
(...)
Then modification for /bootstrap/paths.php is required for line with 'public' key:
'public' => DIR.'/../public' to 'public' => DIR.'/../public_html'
If someone has similar issue and this solution does not work please let me know, thanks.
If you want without public on uri, using composer from your laravel root. Use command php artisan serve and browse localhost:8000 from your browser. It will bring you to homepage.
Where did you get this line from?
RewriteRule ^(.*)$ public/$1 [L]
It's not part of Laravel. Try removing it or the whole .htaccess file in your Laravel root.
Related
I have laravel 9 running locally on windows (MAMP) and I want to open the home page with:
localhost/myproject
instead of
localhost/myproject/public
I tried to add .htaccess to the root folder with the following:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
and it didn't work! (I got 404),
(Although this worked with laravel 8!)
so what is the solution for this !? I spent hours on searching blogs and tweaking to no avail!
I also read on some discussions that using .htaccess or replacing the server.php and renaming it are NOT totally safe for production.
so what is the solution, and would this solution also work if I host my website online (for example on shared hosting) ?
THANKS
I have deployed several websites to hosting and vps servers with following .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
If error still occurs you may contact to your hosting provider and ask them to give you valid .htaccess rules. You may move your files inside public folder to root folder as well.
Please beware of doing solutions like htacces and changing laravel core files in production, which will make your private files public.
there is a more secure way to do it.
Developments
use: `php artisan serve`
production
you can use Document Root to make the domain point to your project's public folder.
To remove the public folder from laravel 9 you need to customize the .htaccess file.
You must have mod_rewrite enabled on your Apache server. The rewrite module is required to apply these settings. You also have enabled .htaccess in Apache virtual host for Laravel.
Update the code into your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
create a .htaccess in root directory
put these two line in it
RewriteCond %{REQUEST_URI} !^/public($|/)
RewriteRule ^(.*)$ public/$1 [L]
I am trying to install Laravel in an old project.
I tried to place my existing project files from /www folder into /public folder.
Now I am facing some issues.
1) I tried to rename the Laravel's index.php to laravel.php and same in .htaccess but it did not work. I am doing this to avoid the conflict with my index.php
2) My project has following /www/.htaccess file and uses routing using mod_rewrite_delegator.php file. I have not idea how to handle this part.
If I user myproject.com/events this does not work but myproject.com/events/dashboard.php works. So I need Laravel to find the route from my old project first and then under Laravel routing.
Here are my existing .htaccess code
Options -Indexes +FollowSymLinks
RewriteEngine on
# quick links
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !/user_files/
RewriteRule ^([^\.\?]+)$ /mod_rewrite_delegator.php?keyword=$1 [QSA,L]
ErrorDocument 404 /http_404.php
Thank you
I have this web api project which is developed by other company. The file structure is:
/project
---/app
---/ApiEndpoint.php
---/public
---/index.php
The DocumentRoot is pointing to /project/public. The index.php is working (http://myapi.com/), however when I try to browse into the api endpoint http://myapi.com/api/endpoint I got 404 error.
How do I configure the .htaccess to rewrite this condition?
/project/public/.htaccess config
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]
</IfModule>
The current configuration is there to make sure every request go to index.php (that is probably dispatching requests in some way) except for the files that actually are in the public directory (probably needed for static files like images, and such).
If you wrote a php yourself to be directly called by http://myapi.com/api/endpoint, you should put it in /project/app/public/api/endpoint/index.php.
BUT I suspect you should study that application more and understand the current dispatching method, before doing that.
I created my application in XAMPP and it worked fine, now when I have shifted it on my website's subdomain. When I try to open the login page of my application it gives error Unable to locate the model you have specified
Directory Structure on subdomain:
public_html/
Subdomain/
Codeigniter/
application/
models/
views/
controllers/
Login Page of my Codeigniter application is:
On local host
http://localhost/INvoice/index.php/admin/Login_Controller/
On website
http://subdomain.example.com/index.php/admin/Login_Controller/
In xampp for codeigniter I use this htaccess with windows 7 & 8.
Options +FollowSymLinks
Options -Indexes
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
Order deny,allow
Deny from all
</FilesMatch>
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I use sub domains on live and that htaccess works fine so long it is in main directory of the subfolder and parent directory. I think because you have deny all which is causing you issue.
Main Directory
Sub Dir Demo
I figured out the problem. Basically I have problems in my file naming convention for models. I have been using Capital letters in File Name i.e. Login_Model.php and when I changed it to login_model.php it is fixed.
Just to you know, My .htaccess is still empty but I have no problem in my URLs.
How can I access a folder placed in the public directory of my default laravel installation. This question is for both - testing as well as live purpose.
While Testing-
I am testing on windows using XAMPP's apache http server. I have placed my app's root folder in the htdocs folder of XAMPP. Thus, when I try to access a folder in public directory, 'localhost/myappname/public/blog' it redirects me to 'localhost/blog'.
I don't want this behavior. I want it to pick up the 'index.php' file present at 'localhost/myappname/public/blog/' location. But it doesn't do so.
While Live-
I have my site live on ubuntu with apache http server. When I try to access 'mysitename.com/blog' in the firefox, it gives me this error -
Firefox has detected that the server is redirecting the request for this
address in a way that will never complete.
I have no hint about what's wrong. I have tried spending two days working with laravel's default .htaccess file(in public folder) and apache httpd.conf but still not able to resolve it. Have searched a lot over stackoverflow and google but still no clue about where I am wrong. Please guide me.
Added:
The content of .htaccess file is -
<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]
You just need to comment this line in your .htaccess file.
RewriteRule ^(.*)/$ /$1 [L,R=301]
Comment it or strip out from the file. This will solve both your directory /blog issue as well as redirect loop issue.
I used the Alias feature of apache to overcome this problem. I added an alias for '/blog' in my virtual host configuration.