htaccess 301使用正则表达式重定向

时间:2011-07-20 17:45:31

标签: .htaccess seo http-status-code-301

这是我当前的.htaccess文件

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/


RewriteRule ^([^/]+)/$ index.php?p1=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?p1=$1&p2=$2&p3=$3&p4=$4&p5=$5 [L]

基本上,它最多需要五个“友好网址文件夹”并将值分配给varibles,然后将它们发送到我的index.php页面

IE: http://www.example.com/ford/focus/grey/

$p1 = 'ford';
$p2 = 'focus';
$p3 = 'grey';
$p3 = 'grey';

到目前为止,非常好。

现在,我需要在同一个.htaccess中集成301指令(RegExp?),因为最初我有这样的GET参数:

IE: http://www.example.com/ford/focus/grey/?lang=fr

我需要摆脱所有GET变量,因为Google将其视为重复内容(即使我在我的语言链接上使用nofollow属性)

IE: http://i.want.to.keep/my/url/?AND_DUMP_GET_VARIABLES

http://www.example.com/ford/focus/grey/?lang=fr
http://www.example.com/ford/focus/grey/?lang=en
http://www.example.com/ford/focus/grey/?lang=sp
==> http://www.example.com/ford/focus/grey/

逻辑上,指令应该在第一个和第二个块之间解释,但我不知道从哪里开始。任何提示?

谢谢!

1 个答案:

答案 0 :(得分:0)

根据我的理解,您希望摆脱QUERY STRING并将重定向(301永久重定向)重定向到相同的网址但不使用QUERY STRING。

以下规则将重定向具有查询字符串的每个请求:

RewriteCond %{QUERY_STRING} !^$
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1? [R=301,L]

1。 ?将完成魔术 - 将剥离查询字符串

2。你迫切需要这一行:RewriteCond %{ENV:REDIRECT_STATUS} ^$。问题是它的可能无法在你的Apache设置上运行而且我无法告诉你它究竟需要什么来使它工作(它在我的vanilla Apache v2.2.17在Windows上工作正常)。

在重写(内部重定向)发生后,它进入下一次迭代,Apache再次开始匹配顶部的所有规则,但已经重写了URL。如果我们不添加上述行,那么mod_rewrite会将上述规则应用于重写的网址表单,最终您将所有网址重写为/index.php,而根本没有任何参数。

如果以上操作不起作用,请尝试以下代码:

# do not do anything for already existing files and folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1? [R=301,L]

借助# do not do anything for already existing files and folders规则,在将URL重写为/index.php?p1=...格式后,mod_rewrite将停止重写。

如果以上内容完全不起作用(更好 - 除了上述内容 - 我建议另外添加此内容)请在您的页面中使用<link rel="canonical" href="FULL_PROPER_RUL"/>

相关问题