How to correctly configure htaccess in subdirectory Laravel? - php

I am currently following this guide to allow the running of our current legacy PHP application inside of Laravel with a view to migrating over totally over time.
https://tighten.co/blog/converting-a-legacy-app-to-laravel/
To this end I have managed to get Laravel to run the legacy application index.php file however I believe there may be some issue with the routing as the URL is not correct and the login page does not load.
Here is my current file structure, I have two .htaccess files one at the root of laravel and one in the root of the legacy folder.
The laravel root htaccess file is rewriting the public folder so it should not show in the URL while working on dev.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
The legacy applications .htaccess file handles all the routing for the views and files of the old system, see below.
Options -Indexes
DirectoryIndex index.php
AddType image/svg+xml svg
AddType image/svg+xml svgz
# HSTS Header - removed https only as AWS lb > server is http
Header set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
# core redirects
RewriteEngine On
RewriteBase /legacy
RewriteRule ^login/?$ /views/login/index.php [QSA,L]
RewriteRule ^app-redirect?$ /callback/apps/redirect_device.php [QSA,L]
RewriteRule ^login/logout?$ /views/login/logout.php [QSA,L]
RewriteRule ^register([/]*)$ /views/login/register.php [QSA,L]
RewriteRule ^login/change-password([/]*)$ /views/login/change_password.php [QSA,L]
RewriteRule ^authenticate/auth-check([/]*)$ /views/login/auth_check.php [QSA,L]
It also appears to be affecting the public URL rewrite when accessing the application as it adds public back into the URL like so:
system.co.uk/public/login
when it should be
system.co.uk/login
Summary:
So I have it successfully running the index.php file located in the legacy folder however it just stops there it doesn't appear to be taking any of the routes in the legacy .htaccess file like /login into the account.
Does anyone have any idea what could be going on here?

the htaccess file under the legacy folder shouldn't affect the root folder.
redirects can be cached by your browser, have you tried other browser or used incognito mode?

Related

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/

Apache vHost and API "pretty url" getting 404

I have a REST API that previously had urls on my localhost that looked like:
localhost/api/v1/collection/resource
As I'm working on an API and web app locally, I implemented apache virtual hosts to get an api url of:
api.mydomain.com/v1/collection/resource
I have a front controller that I'm routing calls. api.mydomain.com hits the front controller successfully. api.mydomain.com/v1 lists the directory (all the php files), which is a problem, and api.mydomain.com/collection/resource results in a 404.
My directory structure is:
/api
/v1
php files
.htaccess
Router.php
index.php
.htaccess contains:
RewriteEngine On
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I'm not quite sure where the issue lies. Anything other than the api.mydomain.com seems to bypass hitting index.php. I'm not too familiar with these rewrites, so any insight is appreciated.
Try this
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^api.mydomain.com$ [NC]
RewriteRule ^(.*)$ index.php [R=301,NC,L]
Found the issue. When I created the vHosts I copied an incorrect setting of AllowOverride None, which was rendering my htaccess files ineffective. Changing to AllowOverride All under the associated vHost directory resolved this.

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.

Apache Rewrite rule with Laravel not working

I have the following directory structure:
/home/username/public_html
my_laravel_app/
.htaccess
app/
bootstrap/
public/
.htaccess
index.php
robots.txt
...etc (standard Laravel 4 public folder contents)
vendor/
...etc (standard Laravel 4 files and folders)
I am trying to use an apache RewriteRule to redirect a request to http://example.com/~user/my_laravel_app to http://example.com/~user/my_laravel_app/public/index.php. In the first .htaccess file (the one directly under the my_laravel_app/ directory, I have the following code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~username/my_laravel_app
RewriteCond %{THE_REQUEST} !public/
RewriteRule ^(.*)$ public/index.php [R]
</IfModule>
When I navigate my browser to http://example.com/~username/my_laravel_app, I correctly get redirected to http://example.com/~user/my_laravel_app/public/index.php, and the laravel application loads as expected displaying the route for / as defined in app/routes.php.
However, I want the redirect to be internal, meaning, I don't want the users browser to display the new location. So, I simply remove the [R] flag on the rule. The .htaccess file now contains:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~username/my_laravel_app
RewriteCond %{THE_REQUEST} !public/
RewriteRule ^(.*)$ public/index.php
</IfModule>
Now, when I direct the browser to http://example.com/~username/my_laravel_app I get a Laravel whoops error page saying there was a NotFoundHttpException. When I look at the error page, it seems as though the redirect is "working", because I see that the SCRIPT_NAME is /~username/my_laravel_app/public/index.php.
However, I believe the issue is possibly that the REQUEST_URI is /~username/my_laravel_app/, instead of '/', as it would be if Laravel were installed in the root of a domain like normal. So it seems as though Laravel is looking for a route match using this REQUEST_URI variable.
Is there any way to get Laravel to recognize the correct route?
I had the same issue on Laravel 4.1, and appears related to my apache config, however, I did find a workaround which was to add the following to the top of the public/index.php:
$_SERVER['SCRIPT_NAME'] = 'path/to/laravel/index.php';
// note: this is a workaround for mod_rewrite and laravel which updates the script_name from /path/to/laravel/public/index.php to /path/to/laravel/index.php. I forget exactly why this worked, but I think when I traced the problem back it was related to something in a symfony dependency.

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