【问题标题】:Best way to remove public and index.php from Laravel 5 URLS从 Laravel 5 URLS 中删除 public 和 index.php 的最佳方法
【发布时间】:2015-12-13 22:47:49
【问题描述】:

由于我是 laravel 的新手,我已经安装了一个新副本,授予必要的权限并运行应用程序,一切都很好。 现在我已经在 apache 中创建了一个虚拟主机来删除公共,这也可以正常工作:

<VirtualHost *:80>
ServerName mobileapp

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/mobileapp/public
<Directory "/var/www/html/mobileapp/">
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/mobileapp_error.log
CustomLog ${APACHE_LOG_DIR}/mobileapp_access.log combined

现在该站点可以通过http://mobileapp/index.php/pages 在本地访问,但如果我从 url (http://mobileapp/pages) 中删除 index.php 将无法正常工作。 我的 .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]           # <----- Tested not working
RewriteRule ^(.*)$ /$1 [L]             # <----- neither this one

我还启用了 mode_rewrite。谁能告诉我什么是最好和最简单的骑行方式。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite laravel-5


    【解决方案1】:

    我只是将server.php 重命名为index.php,它就可以工作了

    【讨论】:

      【解决方案2】:

      试试这个前端控制器规则:

      RewriteRule ^index\.php(/|$) - [L,NC]
      
      # Handle Front Controller...
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^([^.]+)/?$ index.php/$1 [L]
      

      【讨论】:

      • 它在日志文件中给出了这个错误:由于可能的配置错误,请求超出了 10 个内部重定向的限制
      • 确定检查更新的答案,如果您的 htaccess 中有更多规则,请更新您的问题
      • 是的,它现在工作正常.. 谢谢。实际上,我已经将两个 .htaccess 文件放在根目录中,而另一个放在公共目录中。现在我删除了根目录(因为 DocumentRoot 是公共目录),然后将 .htaccess 更改为旧的,工作正常......但你的一个也可以在最坏的情况下工作,谢谢伙计...... :)
      【解决方案3】:

      要调试重写,请将以下内容添加到您的 .htaccess

      RewriteLog ${APACHE_LOG_DIR}/rewrite.log
      RewriteLogLevel 9
      

      然后您可以为您的 RewriteRules 尝试不同的正则表达式,看看会发生什么。我会尝试以下方法:

      RewriteRule ^/index.php(.*)$ http://mobileapp/$1 [L]
      

      请记住,要应用 RewriteRule,首先需要满足 RewriteCond 行。在您的情况下,您正在执行!-d!-f 的条件,所以这可能是您的问题。

      【讨论】:

      猜你喜欢
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 2015-07-16
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多