【问题标题】:URL Rewriting and Laravel 4 Routes not working on server?URL 重写和 Laravel 4 路由在服务器上不起作用?
【发布时间】:2015-07-08 01:18:01
【问题描述】:

我敢打赌,这个问题已经被问过一遍又一遍,但我似乎无法得到答案。无论如何,这是我的情况。我有一个域,比如说 example.com。 Example.com 工作正常,一切正常。但是,如果我导航到在我的本地计算机上运行的 Example.com/about,我会收到 404 错误。但是,如果我访问 example.com/index.php/about,about 页面可以正常工作。我知道这一定是一个mod重写问题。我已启用 mod rewrite 并重新启动我的服务器无济于事。无论如何,这里有一些代码:

虚拟主机:

<VirtualHost *:80>
  ServerName example.com
  DocumentRoot "/var/www/example/public"
  <Directory "/var/www/example/public">
    AllowOverride all
    Allow from All
  </Directory>
</VirtualHost>

这是我的 .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]
</IfModule>

我在 Digital Ocean Droplet 上使用 Ubuntu 14.04。

谢谢!

【问题讨论】:

  • 快速提问,.htaccess 是在 /var/www/example/public 目录中还是在 /var/www/example/ 中?还要检查 .htaccess 文件的文件权限,因为 php 可能无法访问它。
  • 它在 /var/www/example/public
  • 所以我敢打赌这是一个文件权限问题。我以前在 Laravel 上遇到过这种情况。根据您执行 chmod、chown 的位置和方式,您很可能没有包含隐藏的 .htaccess,因为它以 .
  • 我应该将 .htaccess 文件权限更改为什么?
  • php 服务器可以访问的东西。您当前的文件权限是什么? ls -l -a

标签: php apache .htaccess mod-rewrite laravel


【解决方案1】:

1) 转到 /etc/apache2/sites-available 并创建文件并在下面添加

<VirtualHost *:80>
    ServerName example.com
    ServerAdmin webmaster@newyear
    DocumentRoot /var/www/html/example/public
    <Directory /var/www/html/example/public>
        options Indexes FollowSymlinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

2) 创建上述文件的链接并将其粘贴到 /etc/apache2/sites-enabled

3) 检查 /etc/apache2/mods-available 是否有 rewrite.load 文件 如果它不存在,则创建它并添加以下行并保存

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

创建rewrite.load文件的链接并粘贴到etc/apache2/mods-enabled

4) 打开 /etc/hosts 在下面添加

127.0.0.1    example.com

5) sudo service apache2 restart

【讨论】:

  • 如果您无权使用此命令打开文件管理器根模式,则可以右键单击文件和Make Link 执行此命令 `sudo nautilus' 并执行此操作 :)跨度>
  • 我只能在我的服务器上使用命令行,那里还能用吗?
【解决方案2】:

我想出了如何做到这一点,我不得不将我的 VirtualHost 代码放在站点启用,而不是站点可用。掌心。

【讨论】:

  • 惯例是在sites-available 中创建一个文件并将指向该文件的链接放在sites-enabled 中,您可以使用ln -s path_to_file Path_to_link_location 再次检查我的答案
【解决方案3】:

在带有apache2的ubuntu 14.04中我遇到了这个问题。然后我解决了这个问题并写了一个博客,你可以关注这个
http://www.kingpabel.com/apache-mod_rewrite/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-16
    • 2015-02-09
    • 2018-06-11
    • 2015-04-01
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多