Skip to content

Instantly share code, notes, and snippets.

@atomjoy
Last active September 3, 2024 11:25
Show Gist options
  • Select an option

  • Save atomjoy/56992dc72c014fae4e6936df9d1cb647 to your computer and use it in GitHub Desktop.

Select an option

Save atomjoy/56992dc72c014fae4e6936df9d1cb647 to your computer and use it in GitHub Desktop.
Laravel 11 redirect root to public or create public_html storage link.

Redirect root to public in Laravel 11

Shared hosting Laravel rewrite root to public directory create .htaccess in Laravel application directory.

Htaccess

# Shared hosting Laravel rewrite root to public directory

# Php 8.2 Ovh
# SetEnv PHP_VER 8_2
# SetEnv REGISTER_GLOBALS 0

# Php 8.2 Small.pl hosting
AddType application/x-httpd-php82 .php

DirectoryIndex index.php
Options -Indexes -MultiViews +SymLinksIfOwnerMatch
# Options -Indexes -MultiViews +FollowSymlinks

# That was ONLY to protect you from 500 errors if your server did not have mod_rewrite enabled
#<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Root to public
    RewriteRule ^$ public/index.php [L]
    RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
    RewriteRule (^\.|/\.) - [F]

    # Ssl without www (copy to public .htaccess)
    #RewriteEngine On
    #RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    #RewriteRule ^(.*)$ https://%1/$1 [L,R=301,NC]

    # SSL http to https (copy to public .htaccess)
    #RewriteEngine On
    #RewriteCond %{SERVER_PORT} 80
    #RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

    # Trailing slash (copy to public .htaccess)
    #RewriteEngine On
    #RewriteCond %{REQUEST_FILENAME} !-d
    #RewriteRule ^(.*)/$ /$1 [R=301,L,NC]
#</IfModule>

#<IfModule mod_rewrite.c>
    # Directory deny access
    RewriteEngine on
    RewriteRule ^(logs|storage|backup) - [F,NC]
#</IfModule>

<Files .env>
    Order allow,deny
    Deny from all
</Files>

<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

<FilesMatch ".(jpg|jpeg|png|gif|ico|webp)$">
    Header set Cache-Control "max-age=86400, public"
</FilesMatch>

Htaccess mini

# That was ONLY to protect you from 500 errors if your server did not have mod_rewrite enabled
#<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteBase /
	RewriteCond %{REQUEST_URI} !/public
	RewriteRule ^(.*)$ public/$1 [L]
#</IfModule>

Replace public to public_html in Laravel (Example)

Only if you can add Laravel files below public_html in hosting with FollowSymlinks (to dla small.pl gdy dodasz envs dir dla domeny).

Filesystem create symlinks

php artisan storage:link

<?php
// config/filesystems.php

return [
	'links' => [
		public_path('storage') => storage_path('app/public'),		
		// Works with vue
		base_path('public_html') => base_path('public'),
	],
];

Service provider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Event;

class AppServiceProvider extends ServiceProvider
{
	public function register(): void
	{
		// Rewrite public dir
		app()->usePublicPath(app()->basePath('public_html'));
		
		// Or
		// app()->bind('path.public', function () {
		// 	return base_path() . '/public_html';
		// });
	}
}

Symlinks

php artisan storage:link
php artisan config:clear

Virtualhost local domain

# Doamin here# Add host in: C:\Windows\System32\drivers\etc
# 127.0.0.11 app.xxx www.app.xxx

<VirtualHost 127.0.0.11:80>
    DocumentRoot "D:/www/app.xxx/public_html"
    DirectoryIndex index.php
    # Doamin here
    ServerName app.xxx
    ServerAlias www.app.xxx

    #ErrorLog "D:/www/app.xxx/storage/logs/app.xxx.error.log"
    #CustomLog "D:/www/app.xxx/storage/logs/app.xxx.access.log" common

    # Redirect ssl
    #RewriteEngine On
    #RewriteCond %{HTTPS} off
    #RewriteRule (.*) https://%{SERVER_NAME}$1 [R,L]

    <Directory "D:/www/app.xxx/public_html">
        Options -Indexes FollowSymLinks -MultiViews
        AllowOverride all
        Order Deny,Allow
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost 127.0.0.11:443>
    DocumentRoot "D:/www/app.xxx/public_html"
    # Doamin here
    ServerName app.xxx
    ServerAlias www.app.xxx

    SSLEngine on
    SSLCertificateFile "conf/ssl.crt/server.crt"
    SSLCertificateKeyFile "conf/ssl.key/server.key"

    <Directory "D:/www/app.xxx/public_html">
        Options -Indexes FollowSymLinks -MultiViews
        AllowOverride all
        Order Deny,Allow
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment