.htaccess - htaccess:隐藏文件夹并隐藏子文件夹链接中的文件扩展名
问题描述
我有这些文件夹结构:
domain.com/pages/index.php
domain.com/pages/about/a-page.php
domain.com/pages/service/service-type/service-name.php
我希望他们像:
domain.com/index
domain.com/about/a-page
domain.com/service/service-type/service-name
这些是我到目前为止所拥有的:
#remove folder name on link e.g. https://example.com/pages/file.php will become https://example.com/file.php
RewriteEngine On
RewriteRule ^$ pages/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ pages/$1
#remove php file extension-e.g. https://example.com/file.php will become https://example.com/file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
老实说,我只是从教程中复制了这个..并尝试了与我的类似问题的其他建议,这些是我能达到的最接近目标的建议。
解决方案
在看了很多 htaccess 教程之后,我发现它应该是
RewriteEngine On
#remove php file extension-e.g. https://example.com/file.php will become https://example.com/file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
#remove folder name on link e.g. https://example.com/pages/file.php will become https://example.com/file.php
RewriteRule ^$ pages/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ pages/$1 [NC,L]
所以我不应该忘记重写规则中的 [L] 部分,因为它不会读取其他不适合它的重写条件。
推荐阅读
- node.js - 带有图像、视频和其他文件的 MongoDB 聊天记录
- excel - 搜索值时出现通配符问题
- database - 如何使用 SQL-Developer IDE 在 Windows 10 上安装和启动 Oracle Database Express Edition (XE)
- unit-testing - 使用带有表单输入元素的 ReactBootstrap 模式对 React 组件进行单元测试
- subprocess - 在 python 脚本中运行 gcloud auth 和 gsutil 命令
- selenium - 如果未找到输入字段,则跳过代码执行
- c# - 更改 WPF 中 MaterialDesign DatePicker 的外观
- xml - 如何根据Scala中不同的XML属性名称传递自定义模式
- php - PHP 如何使用 appVersion 3.2 将 XML 请求发布到服务器(TNT Express Connect 定价模块)?
- python - 在 VS 代码 Jupyter Notebook 中创建目录