【问题标题】:.htaccess setting: Laravel Page redirect is not working.htaccess 设置:Laravel 页面重定向不起作用
【发布时间】:2018-02-03 11:26:11
【问题描述】:

这是我遇到问题的我的 Laravel-5.4 代码。我创建了测试 laravel 项目,其中包含 home关于我们 两个页面,并使用 Apache v-host(www.test.com)
映射了文件路径 当我点击 www.test.com 时,它会正确加载到索引页面,但是当我尝试点击 www.test.com/about em> 它显示 404 错误。但是当我点击这个 url www.test.com/index.php/about 页面加载正确 我不知道它的问题出在我的代码或 Apache 配置或任何其他方面

控制者:

public function index(){
    return view('pages.index');
}
public function about(){
    return view('pages.about');
}

路线:

// Home Page
Route::get('/','pagesController@index');
// About Us Page
Route::get('/about','pagesController@about');  

Apache 配置:

   ServerAdmin webmaster@localhost
   ServerName test.com
   ServerAlias www.test.com
   DocumentRoot /var/www/html/test/public/

.htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    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>

【问题讨论】:

  • 可以分享public目录下的.htaccess文件吗?
  • @BagusTesa my .htaccess Options -MultiViews RewriteEngine On # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301] # 处理前端控制器... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L ] # 处理授权标头 RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
  • 请将其添加到您的问题中..
  • @HarishKarthick :请使用此详细信息编辑您的问题! (即 .htaccess)
  • @BenjaminBrasseur 好的

标签: php apache laravel web laravel-5


【解决方案1】:

您是否在 apache 上安装并启用了 mod_rewrite? 为了使用mod_rewrite,您可以在终端中输入以下命令:

a2enmod rewrite

之后重启apache2

/etc/init.d/apache2 restart

service apache2 restart

这是我的public/ 目录中的.htaccess

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

另外,编辑/etc/apache2/sites-enabled/000-default

<Directory /var/www/>
   Options Indexes FollowSymLinks MultiViews
   AllowOverride All
   Order allow,deny
   allow from all
</Directory>

【讨论】:

  • 我重写并重新启动 apache 仍然面临问题
  • @HarishKarthick 你在 中添加了AllowOverride all 吗??
  • 在目录中的 AllowOverride all 之后,它可以完美运行 感谢兄弟,它可以正常工作!非常感谢
  • 如果我的回答有效,请点赞!这样其他用户可能会得到正确的解决方案!
  • 我有 11 名声望 15 需要 抱歉,再次感谢你们
【解决方案2】:

您可能有一个未启用的 mod_rewrite 问题。

尝试用a2enmod rewrite启用它,然后重启apache /etc/init.d/apache2 restart

看看answer

如果它不起作用。
这可能来自 Apache conf:etc/apache2/httpd.conf
这个article会比我解释得更好。

您可能需要编辑:

<Directory "/var/www/html">
    ...
    AllowOverride None
    ...
</Directory>

<Directory "/var/www/html">
    ...
    AllowOverride All
    ...
</Directory>

然后重启apache/etc/init.d/apache2 restart

如果还是不行,试着在你的 Laravel 项目上做一些chmod 0755

【讨论】:

  • 我添加了另一种可能的解决方案。希望这会奏效。
  • 很高兴为您提供帮助!不要忘记验证答案;)
  • 非常感谢
猜你喜欢
  • 2018-02-15
  • 1970-01-01
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多