【问题标题】:/ in url not working in laravel/ 在 url 在 laravel 中不起作用
【发布时间】:2017-04-11 19:09:09
【问题描述】:

http://localhost/projectname/admin 工作正常,但是当我尝试使用 http://localhost/projectname/admin/ 实现相同功能时,它会给出 “找不到对象!” 错误 404。

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

那么如何在 url 中使用尾随 / 来解决这个问题?提前致谢。

【问题讨论】:

标签: php .htaccess mod-rewrite laravel-5 url-routing


【解决方案1】:

尝试在您的 .htaccess 文件中添加类似此代码的内容

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

也试试这个...

RewriteCond %{REQUEST_URI}  !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

【讨论】:

  • 我试过但没有用。我应该用这个替换我当前的 htaccess 还是只需要附加它或覆盖任何现有的行?
【解决方案2】:
Replace your .htaccess confg with
<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]

</IfModule>

【讨论】:

  • thnx 回答但仍然输出与以前相同。请给我其他解决方案。
【解决方案3】:

在您的 apache 配置文件中将 AllowOverride 设置为 All 然后重新启动您的 apache,它应该可以正常工作。

DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options FollowSymLinks Multiviews
    MultiviewsMatch Any

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

PS:

sudo service apache2 重启

【讨论】:

  • 并使用 laravel 默认的 htaccess 文件。
猜你喜欢
  • 1970-01-01
  • 2020-09-02
  • 2021-05-22
  • 1970-01-01
  • 2019-03-22
  • 2014-11-01
  • 1970-01-01
  • 2017-10-22
  • 2014-04-08
相关资源
最近更新 更多