首页 > 解决方案 > 在 slim 中设置 .htaccess 文件

问题描述

我在使用 .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

虚拟主机 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 的末尾以使其工作。谢谢

标签: phpslim

解决方案


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

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

推荐阅读