【问题标题】:www to non-www with removing extensions htaccesswww 到非 www 并删除扩展 htaccess
【发布时间】:2015-06-18 11:03:54
【问题描述】:

我首先在我的网站上删除了 .php 扩展名。然后我将 www 转发到非 www 版本。但这有一个问题。

我的 .htaccess 文件如下所示:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^188\.166\.104\.194
RewriteRule (.*) http://example.com/$1 [R=301,L]

www 使用该 .htaccess 转发到非 www。太棒了。但问题出在其他文件上。

我现在使用:http://example.com/contact 而不是:http://example.com/contact.php

但是当您尝试打开 http://www.example.com/contact 时,.htaccess 会将我转发到 http://example.com/contact.php/

我该如何解决?

祝你有美好的一天!

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    改变规则的顺序和一些重构:

    Options +FollowSymLinks -MultiViews
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^(www\.example\.com|188\.166\.104\.194)$ [NC]
    RewriteRule (.*) http://example.com/$1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ /$1/ [R=301,L]
    
    RewriteRule ^([^/]+)/$ $1.php [L]
    
    RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]
    

    确保在清除浏览器缓存后进行测试。

    【讨论】:

      猜你喜欢
      • 2017-05-30
      • 1970-01-01
      • 2016-01-07
      • 2023-04-03
      • 2012-07-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多