.htaccess错误很难找到...

时间:2014-11-03 01:01:08

标签: apache .htaccess mod-rewrite redirect

代码

<IfModule mod_rewrite.c>
# Domain re-direction
RewriteEngine On
RewriteCond %{HTTP_HOST} ^stackexchange\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.stackexchange\.net$
RewriteRule ^/?$ "https\:\/\/www\.stackexchange\.org\/“ [R=301]
#
#Support page re-direction
RewriteCond %{REQUEST_URI} ^thesupport/?$ [NC]
RewriteRule ^thesupport/?$ https\:\/\/www\.stackexchange\.org\/support\/ [R=301]
#
#Privacy page re-direction
RewriteCond %{REQUEST_URI} ^privacy/?$ [NC]
RewriteRule ^privacy/?$ https\:\/\/www\.stackexchange\.org\/privacy\/ [R=301,L]
</ifModule>

问题

  • 301从 stackexchange.net 重定向到 stackexchange.org 正在运作
  • 301从 stackexchange.net/thesupport 重定向到 stackexchange.org/support 不起作用(总是给出404页)

即使将%{REQUEST_URI}替换为:

,问题也没有区别
%{THE_REQUEST}
%{REQUEST_FILENAME}
%{REMOTE_URI}

我已经过http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708但似乎无法找到错误的来源。

关于我的阴影代码可能出错的任何想法。

感谢。

2 个答案:

答案 0 :(得分:0)

问题出在这种情况:

RewriteCond %{REQUEST_URI} ^thesupport/?$ [NC]

由于REQUEST_URI匹配带有前导斜杠的URI。所以你的纠正条件应该是:

RewriteCond %{REQUEST_URI} ^/thesupport/?$ [NC]

然而,查看您的规则,您甚至不需要条件,并且可以在RewriteRule本身处理此问题。

<IfModule mod_rewrite.c>
# Domain re-direction
RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?stackexchange\.net$
RewriteRule ^/?$ https://www.stackexchange.org [R=301,L]

#
#Support page re-direction
RewriteRule ^thesupport/?$ https://www.stackexchange.org/support/ [R=301,L]

#
#Privacy page re-direction
RewriteRule ^privacy/?$ https://www.stackexchange.org/privacy/ [R=301,L]
</ifModule>

答案 1 :(得分:0)

解决(@anubhava)如下:
- 使用@anubhava
建议的重定向代码 - 重定向代码被移动到.htaccess文件的顶部,因此它在WordPress重写规则之前出现 - RewriteBase也添加到代码中。

<IfModule mod_rewrite.c>
# Domain re-direction
RewriteEngine On
ReWriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?stackexchange\.net$
RewriteRule ^/?$ https://www.stackexchange.org [R=301,L]

#
#Support page re-direction
RewriteRule ^thesupport/?$ https://www.stackexchange.org/support/ [R=301,L]

#
#Privacy page re-direction
RewriteRule ^privacy/?$ https://www.stackexchange.org/privacy/ [R=301,L]
</ifModule>

.
.
.

# BEGIN WordPress