【发布时间】:2021-05-20 13:48:40
【问题描述】:
在搜索 StackOverflow 并尝试多种正则表达式方法后,我似乎无法获得一个重定向,该重定向将使网站上的每个 example.com/example.html 页面都指向简单的 example.com/example。我的 .htaccess 文件如下所示:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
我的第一次尝试是
RedirectMatch 301 (.*)\.html$ https://example.com$1
虽然它适用于单个 .html 页面,但它会导致主页重定向到 https://example.com/index,即 404。
我最近的尝试是在底部添加这个
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
那根本行不通。然后我将同一行放在该行之前
RewriteRule . /index.php [L]
所以块看起来像这样
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteRule . /index.php [L]
但这只会导致网站加载非常缓慢,并且在加载图像和样式表时会损坏。
有没有我不理解的方法?
【问题讨论】:
标签: regex .htaccess redirect mod-rewrite