gpt4 book ai didi

添加斜杠时,Laravel 在 URL 中恢复为/public/

转载 作者:行者123 更新时间:2023-12-04 07:37:11 24 4
gpt4 key购买 nike

这是我的.htaccess在应该从 URL 中删除“public”的根目录中:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
这是我的 .htaccess在应该去掉斜杠的公共(public)目录中:
<IfModule mod_rewrite.c>
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
</IfModule>
这一切都很好,除了当我输入一个带有斜杠的 URL 时,它会重定向回一个没有斜杠的 URL,而且还有 /public添加。
例如,我们有这条路线:
Route::get('/de', 'HomeController@index')->name('home');
通常,该 URL 类似于 domain.com/de。
但是,如果我输入 domain.com/de/进入浏览器,它会重定向到 domain.com/public/de而不是 domain.com/de - 页面正常加载。
我该如何解决?

最佳答案

您的站点根目录 .htaccess 应该是这样的:

RewriteEngine On

# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301,NE]

# route all requests to public/
RewriteRule ^(.*)$ public/$1 [L]
然后里面 public/.htaccess有这个代码:
RewriteEngine On

# if /public/ is part of original URL then remove it
RewriteCond %{THE_REQUEST} /public/(\S*) [NC]
RewriteRule ^ /%1 [L,NE,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
确保在测试此更改之前完全清除浏览器缓存。

关于添加斜杠时,Laravel 在 URL 中恢复为/public/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67671433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com