htaccess的:重定向仅.html文件到网站根目录根目录、重定向、文件、网站

2023-09-02 20:34:59 作者:绝版、相公

我最近升级的静态站点到CMS,我想老网站的所有html文件重定向到该站点的根目录(http://www.url.com),而不会影响CMS PHP。

I've recently upgraded a static site to a CMS and I'd like to redirect all the html files of the old site to the site's root (http://www.url.com), without affecting the CMS' PHP.

目前htaccess的看起来像这样

Current htaccess looks like this

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>

显然,无论新规则我想补充不得与此相冲突。

Obviously whatever new rules I add mustn't conflict with this.

我想旧文件(url.com/page.html)重定向到url.com。

I'd like old files (url.com/page.html) to redirect to url.com.

我试图寻找,但没有任何工程之后的几件事情。任何帮助AP preciated。

I've tried a few things after searching but nothing works. Any help appreciated.

推荐答案

您需要将之前您的CMS规则添加这些规则

You need to add these rules before your CMS rules:

RewriteEngine On

# check that the request isn't for a legitimate existing resource
RewriteCond %{REQUEST_FILENAME} !-f

# redirect the .html request to the site root
RewriteRule ^(.*).html$ / [L,R=301]