首页 > 解决方案 > 带有否定条件规则的 RewriteCond?

问题描述

我正在尝试将在 Google 中编入索引的所有 URL 重写example.com/zzz.phpexample.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 的想法吗?

标签: apache.htaccessredirectmod-rewrite

解决方案


您可以创建一些例外(根据需要)。例如:

# 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.phpexample.com/zzz/

请注意,上面的重定向将重定向到example.com/zzz- 没有尾部斜杠。


推荐阅读