php - 6 .htaccess 重写 : Remove index. html,删除 .html,强制非 www,强制尾部斜线

标签 php regex wordpress .htaccess mod-rewrite

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

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

  2. Wordpress 安装在子词典 www.domain.com/blog/

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

上面的代码做

  1. 将 www 重定向到非 www
  2. 删除末尾的斜杠(如果存在)
  3. 删除 index.html
  4. 删除所有.html
  5. 重定向 301 到文件名,末尾没有斜杠
  6. 为 wordpress 强制使用尾部斜杠,并从末尾的非尾部斜杠重定向 301

编辑

#removing trailing slash Rule usage

removing trailing slash Rule

最佳答案

对站点根目录 .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/

相关文章:

php - 每月自动重置的引用编号

python - 删除 pandas 系列中以特定字符串开头的所有文本

css - 新闻 |内容区域不会自动展开

php - Wordpress 2nd laugauge 不工作

php - 页面加载时如何转到 history.back()?

php - 如何使用来自 mysql 的数据填充 android 微调器

javascript - 用于匹配 package.json 内容中的模块的正则表达式

python - 如何在 Python 中将这些字符串解析为前缀表示法的元组?

html - 列表上方的菜单白线

javascript - 从模态表单中检索值并更新数据库 php