![如何为子域名上的 laravel 应用设置正确的.htaccess?](https://linux22.com/image/777822/%E5%A6%82%E4%BD%95%E4%B8%BA%E5%AD%90%E5%9F%9F%E5%90%8D%E4%B8%8A%E7%9A%84%20laravel%20%E5%BA%94%E7%94%A8%E8%AE%BE%E7%BD%AE%E6%AD%A3%E7%A1%AE%E7%9A%84.htaccess%EF%BC%9F.png)
我将 Laravel 应用程序从开发本地主机移至互联网生产子域。在本地主机上一切正常。但当我将应用程序移至子域时,.htaccess 似乎无法正确处理重定向。当我在浏览器中输入时,sub.mydomain.com
它会重定向到sub.mydomain.com/public
。主页呈现。但当我转到登录页面时,它返回 500 错误。路由现在是sub.mydomain.com/public/login
它仅当我手动输入时才有效sub.mydomain.com/public/index.php/login
我不知道为什么会发生这种情况,但在本地主机上它运行正常。
我的根.htaccess:
RewriteEngine on
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
还有我的/public .htaccess
<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
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
您能否告诉我如何正确配置 htaccess 以使其正常工作?
答案1
这是我在公共文件夹选项 -MultiViews 中使用的 .htaccess 配置
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
从 http 重定向到 https :
<VirtualHost *:80>
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/html/public
Redirect Permanent / https://yourdomain.com/
....
....
</VirtualHost>
<VirtualHost *:443>
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/html/public
Redirect permanent http://www.yourdomain.com/ https://yourdomain.com
....
....
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
谢谢!