php - 在 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 的末尾以使其工作。谢谢
解决方案
你做错了。这很简单,只需两行代码:
RewriteEngine On
RewriteRule ^myname/(.*)$ /$1 [L]
推荐阅读
- python - 使用python将列表写入.txt文件时,出现'\n'(反斜杠n)?
- r - 每组相同值的最长连续计数
- java - 我如何在方法中使用(WebElement webdriverWait until element is clickable)并调用该方法以重新使用
- sql - SQL查询查找每种类型的最大计数对应的条目
- python - 后验分布 Pystan 的最高密度区间 (HDI)
- asp.net-core - 我是否需要将 css/js 全部放在 Asp.net Core 的 wwwroort 文件夹中?我正在从旧的 Mvc 项目迁移
- java - 如果未提供,默认别名和密钥库路径是什么
- r - 将函数应用于 data.frame 的每一列并组织输出
- php - 是否可以在没有 PHP 回显的情况下接收 ajax 响应
- laravel-5 - 为什么我不能从餐桌上叫名字?