我要提供一些关于我的网站环境的信息
我在根目录下有静态网页。
Wordpress 安装在子词典 www.domain.com/blog/
我有两个 .htaccess ,一个在根目录中,一个在 wordpress 中 文件夹。
我想
- www 到非所有 URL <下面的代码 DID :)
- 从 url <下面的代码中删除 index.html :)
- 删除所有 .html 扩展名/将 301 重定向到 url 没有 .html 扩展名 <下面的代码 DID :)
- 将尾部斜线添加到静态网页/从非尾部斜线重定向 301 <<我需要帮助
- 将尾部斜杠强制到 Wordpress 网页/从非尾部斜杠重定向 301 <下面的代码 DID :)
一些例子
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/
我在 ROOT 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
最佳答案
对站点根目录 .htaccess 这样设置:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#www to non
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?domain\.com)$ [NC]
RewriteRule ^(.+?)/?$ http://%1/$1/ [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html/?[\s?] [NC]
RewriteRule ^ /%1/ [R=301,NE,L]
#index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://%{HTTP_HOST}/ [R=301,L]
# add a trailing slash to non files
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=301,NE]
# add html internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)/$ $1.html [L]
</IfModule>
确保在测试前清除浏览器缓存。
关于php - 6 .htaccess 重写 : Remove index. html,删除 .html,强制非 www,强制尾部斜线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38733574/