重写规则以添加 .html 扩展名 [英] ReWrite rule to add .html extension
问题描述
我需要一个规则,只要有不"结尾的斜杠,就会添加一个 .html 扩展名.
I need a rule that will add an .html extension whenever there is 'not' a trailing slash.
一位新客户最近更改了电子商务脚本,新版本以不同方式处理 SEO,并更改了他们所有 16,000 多个产品链接.这在站点被重新编入索引之前未被发现,因此我们需要将旧站点重定向到新站点.
A new client recently changed ecommerce scripts and the new version handles SEO differently and changed all of their 16,000+ product links. This was not caught prior to the site being re-indexed so we need to redirect the old to the new..
过去所有产品都有这样的链接domain.com/category/productname但现在domain.com/category/productname.html
All products used to have links like this domain.com/category/productname but are now domain.com/category/productname.html
分类链接没变,都是这样domain.com/category/(带有斜杠)
Category links did not change and all are like this domain.com/category/ (with trailing slash)
推荐答案
@david-wolever 的回答将所有不以 .html(或根)结尾的内容重定向到相同的 URL,并添加了 .html 扩展名,意思是它将 .html 扩展名附加到 CSS 和 JavaScripts 文件之类的内容中,例如它会将/style.css 重定向到/style.css.html 这可能不是您想要的.后面还有空格!可能会导致@greggles 500s 的字符
The answer from @david-wolever redirects everything that does not end in .html (or the root) to the same URL with an added .html extension, meaning it appends a .html extension to things like CSS and JavaScripts files, e.g. it will redirect /style.css to /style.css.html which is not likely what you want. It also has the spaces after ! character which will likely caused @greggles 500s
这会重定向不以后跟 3 或 4 个字母数字字符的点结尾的 URL:
This redirects URLs which do not end in a dot followed by 3 or 4 alphanumeric characters:
RewriteEngine On
RewriteCond %{REQUEST_URI} !.[a-zA-Z0-9]{3,4}
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1.html
或者为了更精细的控制,将您不希望将 .html 附加到例如的扩展名列入白名单
Or for finer grain control, whitelist the extensions you do not want .html appended to e.g.
RewriteCond %{REQUEST_URI} !.(html|css|js|less|jpg|png|gif)$
这篇关于重写规则以添加 .html 扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!