apache - htaccess 用最终的子文件夹重写 url
问题描述
我有这样的网址:
https://mywonderfulsite.com/page
我的 .htaccess 将其重写为
https://mywonderfulsite.com/index.php?source=page
.htaccess 如下:
RewriteEngine on
RewriteBase /
RewriteRule \.(php)$ - [L]
RewriteRule ^index\.html$ - [L]
RewriteRule ^favicon\.ico$ - [L]
RewriteRule ^custom404\.html$ - [L]
RewriteRule ^custom500\.html$ - [L]
RewriteRule ^([^/]+)/?$ index.php?source=$1 [L]
如果 url 包含子文件夹,则此操作失败
https://mywonderfuldomain.com/subfolder/anotherpage
那应该变成
https://mywonderfuldomain.com/index.php?source=/subfolder/anotherpage
我似乎不知道如何处理这种情况。如何解决?
解决方案
问题在于([^/]+)
与除 a 之外的任何内容匹配的模式/
。
像这样使用它:
RewriteEngine On
RewriteRule \.php$ - [L,NC]
RewriteRule ^(?:favicon\.ico|(?:index|custom500|custom404)\.html)$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ index.php?source=$0 [L,QSA]
推荐阅读
- node.js - win32-x64 二进制文件不能在“linux-x64”平台上使用
- c++ - 如何使用谷歌基准测试数据包处理功能?基本上是在状态约束下重复的功能?
- c# - HttpClient 发布调用
- javascript - 使用 jQuery 禁用/启用字段
- java - 在spring boot中发送成功响应后调用另一个服务?
- python - 在图像分割中 Jaccard 指数大于 Dice 系数
- android - RecyclerView 分页时在列表中间跳转
- camunda - Camunda 数据并发问题
- linux - `crontab` 什么时候听?
- php - 单击按钮后使用 PHP 将 HTML 输入到 SQL 查询中