【问题标题】:How to configure .htaccess file for rewrite rules to get rid of extensions, trailing slashes, www?如何为重写规则配置 .htaccess 文件以消除扩展名、斜杠、www?
【发布时间】:2012-01-21 10:46:35
【问题描述】:

这就是我想要的-

当有人进入 site.com/page.php 或 site.com/page 或 site.com/page/ 时,都应该工作相同,地址栏中显示的 url 应该是 site.com/page [ I不喜欢扩展名和斜杠]

我也想去掉网址栏中的 www,因为它是不必要的。

这是我当前的 .htaccess 文件重写规则 -

RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.com
RewriteRule ^(.*)$ http://www.site.com/$1 [R=permanent,L] 

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

这解决了 www 和扩展名的问题,但不是尾随斜杠的问题!

但是,更重要的是,我最近在 site.com/blog 上安装了 WordPress,但每当我尝试访问它时,都会被重定向到 404 页面。当我删除所有重写时它会起作用!

我是否搞砸了我的重写?我不是正则表达式忍者!

【问题讨论】:

    标签: wordpress .htaccess mod-rewrite http-status-code-404 trailing-slash


    【解决方案1】:

    尝试将以下内容添加到现有 .htaccess 文件的顶部,高于任何现有规则,例如WordPress 规则。

    RewriteEngine on
    RewriteBase /
    
    #if the host is not site.com, e.g www.site.com
    RewriteCond %{HTTP_HOST} !^site\.com$ [NC]
    #then redirect to site.com e.g to remove the www
    RewriteRule ^(.*)$ http://site.com/$1 [R=301,L] 
    
    #if the URL ends with a slash
    RewriteCond %{REQUEST_URI} ^/(.+)/$
    #redirect to URL without slash
    RewriteRule . http://site.com/%1 [R=301,L] 
    
    #skip wordpress site which starts with blog
    RewriteCond %{REQUEST_URI} !^/blog [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    

    【讨论】:

    • 谢谢@Ulrich。我编辑为在每条规则中忽略 /blog 以使其正常工作!否则会导致“重定向过多”。现在我已经准备好了一切! :)
    猜你喜欢
    • 2013-03-12
    • 1970-01-01
    • 2012-03-18
    • 2014-10-22
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多