使用 .htaccess 重定向整个网站,但不包括 sitemap.xml 和 robots.txt

Redirect an entire site with .htaccess but exclude sitemap.xml and robots.txt

嗨堆栈溢出。

我使用 php、mysql 和 php 路由器概念创建了一个博客。

所有请求都转到 index.php 那里我动态地从数据库中获取数据然后显示给他们。

我的问题是所有请求都会发送到 index.php 但我需要重定向,除了 sitemaprobots & /admin 文件夹

我的文件夹结构:

-example.com
 -admin
 -assets
 -function
 -views
.htaccess
index.php
config.php
sitemap.php
robots.txt

我的 htaccess 代码:

<filesMatch ".(css|jpg|jpeg|png|gif|js|ico)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>

<IfModule mod_rewrite.c>
RewriteEngine on

#RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
#RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]

</IfModule>

就在 RewriteEngine on 行下方,您可以添加此规则:

RewriteCond %{THE_REQUEST} \s/+(?:sitemap\.php|robots\.txt|admin/) [NC]
RewriteRule ^ - [L]