Laravel URLs not working in subfolder - php

I've finished my laravel project and played it in a subfolder on my domain.
https://example.com/swap
So the above is the root of my directory and when I go there, I do get the index. But now every link I press, I'm redirected to the root of my domain. So I'm getting:
https://example.com/events/1
Instead of https://example.com/swap/events/1
Am I forced to change all URLs by hand in my files are is there a way my htaccess can always redirect to this 'swap' folder and place everything behind that?
I've tried using groups in my routes, without success.

Use url for all your links which are inside views folder:
For subfolder link use:
Sub-Folder Link
For root folder links use this syntax:
Index
There might arise issues with stylesheet and javascript in 'swap" sub-folder links with a 404 error. To fix that, use:
<script type="text/javascript" src="{{asset('js/app.js')}}">
<link rel="stylesheet" type="text/css" href="{{asset('css/app.css')}}">

You need to direct your VHOST to laravel's public folder. This is done for security purposes. Ideally your laravel install is outside the public folder, thus you would have yourdomain.com contain your laravel install and rename your public folder to swap.
IF you really want the name swap in your routes you could use route prefix with groups.

Open .env File and Change Your App Url to https://example.com/swap/ and use routes as links of whole application
All Url's Will Work fine.

When putting a laravel application in a subfolder. You need to modify your .htaccess for subfolder hosting. And add the following:
RewriteBase /swap
This way, the app knows when you go to route {{ url('/hello-world') }}, it will ultimately go to "/swap/hello-world"

Related

Laravel specific folders act as root

I came across problem in laravel, cant find a solution, maybe you could help me out.
I have many folders with HTML documents that are linked (same folder as HTML) with css, some of css are inside assets, includes, root folder.
I can access these html files by going https://website.com/live/1/index.html but the problem is that laravel sees css and other files in website root folder https://website.com/assets/app.css instead of https://website.com/live/1/assets/app.css
I think I could find a solution in htaccess by typing RedirectMatch 301 ^/(assets/.+)$ /live/1/$1 but then it means, that I have to create and edit htaccess everytime for a new folder.
Maybe someone has any solution to this problem ?
You can change your default asset URL with your .env file and add this
ASSET_URL=https://website.com/live/1
it would change your asset() method behavior

how i create relative path alway begining root

I create my web app with php follow mvc model.
When I set path css,img,js,... i use <link rel="stylesheet" href="./public/css/style.css">
in default controller I got css for my page. But when I call action like http://localhost/assignment/controller/action i lost my css. relative path become http://localhost/assignment/controller/action/public/css/style.css or http://localhost/assignment/controller/action/param/public/css/style.css.
Sounds like you might have a configuration or bundler issue. The quickest - not best fix - for developing would be to move your css file to where the public folder is located. You need to find the root folder where that link is calling from and link there.
If you are using a bundler or transpiler, you might need to look in the configs for what is being bundled.
Also make sure to follow this format for local files.
The right way of setting <a href=""> when it's a local file

Requests in subdirectory of document root return 404

I've set the document root of my apache virtual host config file to laravel's public directory. I have an angular 4 app that builds into the public/dashboard-dist directory. I have added this directory as a location to search for views config/view.php like this:
'paths' => [
resource_path('views'),
public_path('dashboard-dist'),
],
And I can redirect the user to the dashboard-index.php in this directory after a successful login. But all asset links in this file (js,css etc.) fail to load and return 404. For example bootstrap.min.css hails from:
http://localhost/assets/css/bootstrap.min.css (404)
When I change the href attribute of this file as:
<link href="/dashboard-dist/assets/css/bootstrap.min.css" rel="stylesheet" />
I get a 200 and the file gets loaded. But I don't want to change all URLs in this angular 4 app and instead have a single server configuration to set subdirectory document root to itself. I'd prefer to use .htaccess in public directory to do this so when I upload the site to real server it will work out of the box. How can I achieve this?
The best solution to this type of problem is to run:
ng build --base-href /dashboard-dist/
to build the angular app. This will add
<base href="/dashboard-dist/">
to the build's dashboard-index.php and all asset links will work from now on.

Style breaks on some Laravel pages

I'm using Laravel 5.3 and on some pages the css isn't loading. I tried the suggestions from this question, but they didn't help.
This is what I did so far:
I installed Laravel on wamp inside www/lblog directory so I access laravel's main page via http://localhost:8080/lblog/
To get rid of the "public" part from the URL I moved the index.php and htaccess files from the "public" directory to the root directory and changed two lines in index.php like this:
require DIR.'./bootstrap/autoload.php';
$app = require_once DIR.'./bootstrap/app.php';
In "resources/views/layouts/app.blade.php" I have the styles linked like this:
I tried to remove the / before the "css", it didn't work that way either.
The main page is ok, but if I go to http://localhost:8080/lblog/login for example, the style is broken.
Don't hack the core, use it as it is which is recommended.
Use laravel's built in helpers for generating URL for your assets that are on public directory
<link rel="stylesheet" href="{{ asset('css/styles.css') }}">
which will properly generate URL for your files that are on public directory

Laravel 5.1 show indexes for existing folders

I have a problem with Laravel 5.1 routing, If I try to make a route with the same name that a folder in the /public folder apache shows me the index of the folder (I have forbidden indexes so it shows a Forbidden message) I don't know if it's impossible to make it work, I have obviously changed the name of the assets folder with the conflictive name in public directory, but it would be awesome if I could name it as I want.
If I wans unclear (because my bad english) the short explanation is the following:
I have a "game" folder in the /public folder of Laravel 5.1
I want to have a route named /game (example.com/game)
When I acces to /game apache shows me the index of the folder, instead of redirect me to index.php as rewrite rules do.
Thanks in advice
In your htaccess file there is something like this: if there is no file or directory for the URL then give the URL to laravel.
Just remove the directory check and it should work fine. (Please post your htaccess file so I can tell you what line you have to remove)

Categories