本文介绍了6.htaccess重写:删除index.html,删除.html,强制非www,强制跟踪斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须提供一些关于我的网站的信息环境


  1. 我在根目录中有静态网页。


  2. Wordpress安装在子目录www.domain.com/blog/


  3. 我有两个.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>

上面的代码可以做到:


  1. 将www重定向到非www

  2. 删除末尾的斜杠(如果存在)

  3. 删除index.html
  4. >
  5. 删除所有.html

  6. 重定向301至文件名,最后不加斜杠

  7. 为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

    1. i have static webpage in the root.

    2. Wordpress installed in sub-dictionary www.domain.com/blog/

    3. 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

    1. redirect www to non-www
    2. Remove trailing slash at the end (if exists)
    3. Remove index.html
    4. Remove all .html
    5. Redirect 301 to filename without trailing slash at the end
    6. 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,强制跟踪斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:56