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:
1. Set Web Server Document Root to public (Recommended)
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:
- Locate your virtual host configuration file (usually in
/etc/apache2/sites-available/or/etc/httpd/conf.d/). - Update the
DocumentRootand<Directory>directives to point to your project'spublicfolder:<VirtualHost *:80> ServerName myhostexample.com DocumentRoot /var/www/myhostexample.com/public <Directory /var/www/myhostexample.com/public> AllowOverride All Require all granted </Directory> </VirtualHost> - Enable rewrite module if you haven't already:
sudo a2enmod rewrite - Restart Apache:
sudo systemctl restart apache2
For Nginx:
- 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 } } - Test the config:
sudo nginx -t - 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:
- Move files from
public/to project root: Copy all files and folders inside yourpublicdirectory (including.htaccess) to the root of your Laravel project. - Update
index.phppaths: Open the newly movedindex.phpfile 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'; - Replace your root
.htaccesswith 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
.envfile: SetAPP_URL=http://myhostexample.com - Verify file permissions: Ensure
storage/andbootstrap/cache/directories are writable by the web server (usuallychmod -R 755 storage bootstrap/cacheor adjust ownership to the server user likewww-data)
内容的提问来源于stack exchange,提问作者Bardan Claudiu
相关产品推荐
相关产品推荐

