apache - 带有否定条件规则的 RewriteCond?
问题描述
我正在尝试将在 Google 中编入索引的所有 URL 重写example.com/zzz.php
为example.com/zzz/
,它适用于以下代码(在 中.htaccess
):
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ https://example.com/$1 [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
但是我有一个新问题,它破坏了我的 ajax 和我的 wp-admin 的一些页面,因为 phpless。
有人对这种情况有负面条件的 RewriteCond 的想法吗?
解决方案
您可以创建一些例外(根据需要)。例如:
# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_URI} !^/wp-admin/
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ https://example.com/$1 [R=301,L]
CondPattern上的!
前缀否定正则表达式。所以上面将排除以 . 开头的请求。/wp-admin/
example.com/zzz.php
至example.com/zzz/
请注意,上面的重定向将重定向到example.com/zzz
- 没有尾部斜杠。
推荐阅读
- c# - Unity C# Sockets 回调,触发事件日志到控制台但不能更改游戏对象属性
- sql - 连接两个表,然后将一个表中的行作为列名转置
- python - 在 Pandas 中使用 pivot_table() 和连接的快速方法
- regex - 使用正则表达式时如何修复if语句总是返回假
- reactjs - 当我刷新页面时反应状态是否改变
- java - SH256 Java中的登录密码
- android - 是否可以过滤 Firestore Recyclerview(无第三方应用程序)?
- javascript - 用jQuery/JS,动态改变类不改变类点击触发?
- reactjs - 如何在不调用函数的情况下将函数设置为状态?
- elasticsearch - 如何在 logstash conf 文件中使用包含?