【问题标题】:setting .htaccess file in slim在 slim 中设置 .htaccess 文件
【发布时间】:2019-02-15 22:53:18
【问题描述】:

我在使用 .htaccess 文件的超薄应用程序中重定向到 index.php 时遇到问题。 如果我在 URL 的末尾添加 index.php,路由就可以工作

所以slimapp.dev/hello/myname 出错了

Not Found The requested URL /hello/myname was not found on this server.

slimapp.dev/index.php/hello/myname 工作时

这是我的 .htaccess 文件

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.php [QSA,L]

我在 Ubuntu 18.04 上使用 Apache 2

文件结构

public_html
           |_index.php
           |_ vendor
           |_.htaccess

virtualHost 000-default.conf

<VirtualHost *:80>
       <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html


    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined


</VirtualHost>

如果我使用php -S localhost:3000,它会按预期工作,但如果我使用 Apache Web 服务器,我最常将 index.php 添加到 URL 的末尾以使其正常工作。谢谢

【问题讨论】:

  • 健全性检查:在您的 Apache 设置中,您将 Document Root 指向哪里?
  • 另外,您确定您的虚拟主机的AllowOverride 设置正确吗?只是为了确定,你能发布你的虚拟主机块吗?
  • 除了@maiorano84 所说的之外,您的.htaccess 是否像示例中那样在克拉(^)和“index.php”之间有空格?如果是这样,您可以尝试删除空格。
  • @Hayden 这很好。额外的空间很可能会导致 OP 看到的问题。
  • 在 Apache 中启用重写 a2enmod rewrite

标签: php slim


【解决方案1】:

你做错了。这很简单,只需要两行代码:

RewriteEngine On
RewriteRule ^myname/(.*)$ /$1 [L]

【讨论】:

  • 阅读问题。您的回答与 OP 的要求无关。
  • @Mariorano84,这是简单的 htaccess 规则,我认为 foliwe83 想要将 url 欺骗为“slimapp.dev/hello/myname”而不是“slimapp.dev/hello/”,即 root应用程序的目录。就是这样。
  • 再次:阅读问题。 OP 没有要求欺骗任何 URL。他的问题是 Apache 没有正确地自动推迟对 index.php 的任何请求,因此如果 URL 结构中没有 index.php,他的 URL 都无法正常工作。更进一步,您所做的只是从所有 URL 中删除 myname 前缀,这甚至不能满足您认为他的要求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-12
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多