一个重写规则不能在htaccess中使用3

时间:2014-03-09 08:25:54

标签: .htaccess

我在htaccess文件中有以下内容。第一个和第二个重定向规则块正在运行,但第三个不是。关于什么可能出错的任何线索?

#   Mod_security can interfere with uploading of content such as attachments. If you
#   cannot attach files, remove the "#" from the lines below.
#<IfModule mod_security.c>
#   SecFilterEngine Off
#   SecFilterScanPOST Off
#</IfModule>

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 500 default

RewriteEngine On

RewriteRule [^/]+/.+-([\d]+)/p([\d]+)/ showthread.php?t=$1&page=$2 [NC,L]
RewriteRule [^/]+/.+-([\d]+)/ showthread.php?t=$1 [NC,L]

RewriteCond ${ErrorMap:$1|Unknown} !Unknown
rewriterule ^(.*\.(html)) ${ErrorMap:$1|404} [R=301]

    #   If you are having problems with the rewrite rules, remove the "#" from the
    #   line that begins "RewriteBase" below. You will also have to change the path
    #   of the rewrite to reflect the path to your XenForo installation.
    #RewriteBase /xenforo

    #   This line may be needed to enable WebDAV editing with PHP as a CGI.
    #RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Options +FollowSymlinks
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

这是不起作用的部分。我正在尝试将所有非www链接重定向到www链接。

Options +FollowSymlinks
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

1 个答案:

答案 0 :(得分:0)

我会申请非www - &gt; www rewriteRules首先。按照您显示的顺序,如果showthread.php规则匹配并重写,则[L]或“最后一条规则”标记将阻止进一步重写。

按此顺序尝试:

RewriteEngine On
Options +FollowSymlinks

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond ${ErrorMap:$1|Unknown} !Unknown
RewriteRule ^(.*\.(html)) ${ErrorMap:$1|404} [R=301]

RewriteRule [^/]+/.+-([\d]+)/p([\d]+)/ showthread.php?t=$1&page=$2 [NC,L]
RewriteRule [^/]+/.+-([\d]+)/ showthread.php?t=$1 [NC,L]

你也可以尝试HTML5 Boilerplate非www - &gt; www规则,它很稳固!

<IfModule mod_rewrite.c>
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    RewriteCond %{HTTP_HOST} !=localhost [NC]
    RewriteCond %{HTTP_HOST} !=127.0.0.1
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
 </IfModule>