You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel部署后如何重定向URL去除public路径

Fix Laravel App Loading from Root Domain Instead of /public/

Hey there! Let's get your Laravel app loading directly from http://myhostexample.com instead of needing the /public/ suffix. Here are the most reliable solutions, ordered by best practice first:

This is the safest approach because Laravel's public folder is designed to be the web-accessible root—keeping sensitive files like .env, vendor, and bootstrap outside the web root prevents direct access to them.

For Apache:

  1. Locate your virtual host configuration file (usually in /etc/apache2/sites-available/ or /etc/httpd/conf.d/).
  2. Update the DocumentRoot and <Directory> directives to point to your project's public folder:
    <VirtualHost *:80>
        ServerName myhostexample.com
        DocumentRoot /var/www/myhostexample.com/public
    
        <Directory /var/www/myhostexample.com/public>
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>
    
  3. Enable rewrite module if you haven't already: sudo a2enmod rewrite
  4. Restart Apache: sudo systemctl restart apache2

For Nginx:

  1. Edit your site's Nginx config (usually in /etc/nginx/sites-available/):
    server {
        listen 80;
        server_name myhostexample.com;
        root /var/www/myhostexample.com/public;
    
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.2-fpm.sock; # Adjust PHP version to match your setup
        }
    }
    
  2. Test the config: sudo nginx -t
  3. Restart Nginx: sudo systemctl restart nginx

2. Adjust Project Structure & Update .htaccess (If Server Config Is Unchangeable)

If you can't modify the web server's document root, you can move the public-facing files to the root directory and update paths accordingly:

  1. Move files from public/ to project root: Copy all files and folders inside your public directory (including .htaccess) to the root of your Laravel project.
  2. Update index.php paths: Open the newly moved index.php file and adjust the paths for autoload and app bootstrap:
    // Before
    require __DIR__.'/../vendor/autoload.php';
    $app = require_once __DIR__.'/../bootstrap/app.php';
    
    // After
    require __DIR__.'/vendor/autoload.php';
    $app = require_once __DIR__.'/bootstrap/app.php';
    
  3. Replace your root .htaccess with this correct version:
    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews -Indexes
        </IfModule>
    
        RewriteEngine On
    
        # Handle Authorization Header
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)/$ /$1 [L,R=301]
    
        # Send Requests To Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </IfModule>
    

Final Checks

  • Update your .env file: Set APP_URL=http://myhostexample.com
  • Verify file permissions: Ensure storage/ and bootstrap/cache/ directories are writable by the web server (usually chmod -R 755 storage bootstrap/cache or adjust ownership to the server user like www-data)

内容的提问来源于stack exchange,提问作者Bardan Claudiu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 08:47:46