.htaccess - 如何将所有对 index.php 和 sitemap.xml 的请求重写为 sitemap.php
问题描述
请帮忙,我已经做了2天了,一切都是徒劳的......
此代码适用于将目录和页面重写为一个 index.php 页面
DirectoryIndex /index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.php [NC,L]
RewriteRule ^/sitemap.xml sitemap.php [L]
sitemap.xml 的问题,因为它必须重写为 sitemap.php 而不是 index.php
解决方案
您需要将站点地图规则放在索引规则之前。规则是从上到下处理的,一旦它达到索引规则(因为.*
会 match sitemap.xml
),它将因为[L]
标志而停止进一步处理。
另一种选择是只使用 php 和 inside index.php
,include sitemap.php;
当$_SERVER['REQUEST_URI']
equals时sitemap.xml
。结果相同。
推荐阅读
- hibernate - Kotlin:即使我全部使用 kotlin-maven 插件,为什么休眠实体类是最终的?
- python - 如何使用 Selenium 在下拉列表中选择项目?
- php - 在第二页上将 php 返回到 html 的问题
- python - FindContours 仅支持 CV_8UC1 图像
- javascript - 关于javascript中while循环的问题
- c# - Google Cloud Vision API - client.DetectText 正在跳过 0
- processing - 处理中的 Draw() 不更新屏幕
- javascript - socket.io 在房间里广播
- sql - 如何在 bigquery 中回填空值?
- c++ - 尝试将字符分配给二维字符数组时,表示数组值的整数在 for 循环中增加