how to get rid of public from url in laravel 3 - php

Hello I am new to laravel and i know laravel has new version but i am using version 3.
Now all i need to do is directly access public folder without typing /pulic in url.
I have placed below .htaccess file in laravel root but it giving me 404 error.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
and i want to do this using .htaccess not using virtualhost.

I think you should make your site's document root point to public from your server config (vhost on apache, not sure what it's called on nginx)

Related

how to remove the /public folder from laravel 9?

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]

Route not working properly with htaccess in laravel

I have created my application using laravel and vue.js and hosted my application on server in subfolder inside public_html,
Accessing it using url like this:
https://maindomain.in/projectfolder/public/login
but after login if i click on any link it points back to
https://maindomain.in/dashboard
and i get 404 not found in console for the api routes that are accessed for eg. accessing dashboard
https://maindomain.in/api/backend/get-dashboatd-data
My root htaccess file content is :
<IfModule mod_rewrite.c>
RewriteEngine on
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove public folder form URL
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Note: Frontend is in Vue.js and backend in laravel.
Any help is highly appreciated
It seems to me like your web server is not pointing to the right folder.
Laravel expects you to serve the project from the public folder inside the project's root directory.
You're serving it from the main apache/nginx folder. That's why you need to add the subfolder and public folders in the URL to visit the website.
Check the Laravel Nginx configuration example to notice the root directory they're specifying.
Your nginx/apache server is configured wrong
You must point nginx/apache to the folder
/var/www/XXX_PROJECT_XXX/

htaccess - Redirect parent folder PHP script

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.

Laravel 5 with cpanel redirect to default language URL

I have a Laravel 5.2 application on a cPanel hosting account in which I had to remove the "public" folder from the URL with the following .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Now I need to redirect everything incoming to just the main domain/page www.example.com to be redirected to www.example.com/en.
I tried some examples but all of them break the local css and img.
Reinventing the wheel is always a bad idea. What you should do is to setup your web server correctly by pointing it to a public directory and restarting it. Also, use original Laravel .htaccess.
After that use Laravel localization.
Then you could just copy Controller#method you using for /en route and put it into / route.

Laravel called from ROOT outsite the /public folder. Getting NotFoundHttpException

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.

Categories