问题描述
我必须提供一些关于我的网站的信息环境
-
我在根目录中有静态网页。
-
Wordpress安装在子目录www.domain.com/blog/
-
我有两个.htaccess,一个在根目录中,一个在wordpress
文件夹中。
p>
- www对所有网址中的非<下面的代码DID它:)
- 从url中移除index.html < )
- 删除所有.html扩展名/将301重定向到url
不带.html扩展名< ) - 在静态网页中添加尾部斜杠/从非尾部斜杠重新定向301 <<我需要帮助
- 强制结束斜杠到Wordpress网页/从非结尾斜杠重新指定301 < / li>下面的代码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 /
#移除尾部斜线
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} ^ [AZ] { 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 /
#为Wordpress强制拖拽Slash
RewriteCond%{REQUEST_FILENAME}!-f
RewriteRule ^(。*)[^ /] {1} $%{REQUEST_URI} / [L,R = 301]
< / IfModule>
上面的代码可以做到:
- 将www重定向到非www
- 删除末尾的斜杠(如果存在)
- 删除index.html >
- 删除所有.html
- 重定向301至文件名,最后不加斜杠
- 为wordpress强制拖放斜杠和在结尾重定向301从非结尾斜线
编辑
#删除尾部斜杠规则用法
解决方案以这种方式为网站根.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} ^ [AZ] {3,9} \ /index\\.html \ HTTP /
RewriteRule ^ index\.html $ http://%{HTTP_HOST} / [R = 301,L]
#向非文件添加尾部斜杠
RewriteCond%{ENV:REDIRECT_STATUS} ^ $
RewriteCond%{REQUEST_FILENAME}!-f
RewriteRule ^(。*?[^ /])$%{REQUEST_URI} / [L,R = 301 ,NE]
#在内部添加html
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}!-d
RewriteRule ^([^。] +)/ $ $ 1.html [L]
< / IfModule>
确保在测试前清除浏览器缓存。
i've to give some information about my website Environment
i have static webpage in the root.
Wordpress installed in sub-dictionary www.domain.com/blog/
I have two .htaccess , one in the root and one in the wordpressfolder.
i want to
- www to non on all URLs < below code DID it :)
- Remove index.html from url < below code DID it :)
- Remove all .html extension / Re-direct 301 to urlwithout .html extension < below code DID it :)
- Add trailing slash to the static webpages / Re-direct 301 from non-trailing slash << I NEED HELP WITH THAT
- Force trailing slash to the Wordpress Webpages / Re-direct 301 from non-trailing slash < below code DID it :)
Some examples
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/
My code in ROOT htaccess is
<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>
The above code do
- redirect www to non-www
- Remove trailing slash at the end (if exists)
- Remove index.html
- Remove all .html
- Redirect 301 to filename without trailing slash at the end
- Force Trailing Slash for wordpress and redirect 301 from non trailing slash at the end
EDIT
#removing trailing slash Rule usage
解决方案Have it this way for site root .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>
Make sure to clear your browser cache before testing.
这篇关于6.htaccess重写:删除index.html,删除.html,强制非www,强制跟踪斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!