我需要提供有关我的网站环境的一些信息。
我在根目录下拥有静态网页。
WordPress安装在子目录www.domain.com/blog/中。
我有两个.htaccess文件,一个在根目录中,另一个在WordPress文件夹中。
我想要:
- 将所有URL中的www删除(以下代码已实现)。
- 从URL中删除index.html(以下代码已实现)。
- 删除所有.html扩展名/重定向301到没有.html扩展名的网址(以下代码已实现)。
- 在静态网页中添加尾部斜杠/重定向301从无尾部斜杠(需要帮助)。
- 强制WordPress网页添加尾部斜杠/重定向301从无尾部斜杠(以下代码已实现)。
一些示例:
domain.tld/index.html >> domain.tld/
domain.tld/file.html >> domain.tld/file/
domain.tld/file.html/ >> domain.tld/file/
domain.tld/wordpress/post-name >> domain.tld/wordpress/post-name/
我的根目录.htaccess中的代码为:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#removing trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
#www to non
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?domain\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
#html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
#index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://domain.com/ [R=301,L]
RewriteCond %{THE_REQUEST} \.html
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /blog/
# Force Trailing Slash for wordpress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)[^/]{1}$ %{REQUEST_URI}/ [L,R=301]
</IfModule>
以上代码的作用是:
- 将www重定向到非www
- 去除结尾的斜杠(如果存在)
- 删除index.html
- 删除所有.html
- 将301重定向到没有结尾斜杠的文件名
- 强制使用WordPress的尾随斜杠,并从结尾没有斜杠的地方进行301重定向
编辑
#removing trailing slash Rule usage
/blog/.htaccess
中而不是主.htaccess
中。你想要添加尾随斜杠的示例 URL 是什么? - anubhava