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

.htaccess 使用正则表达式的htaccess 301重定向,.htaccess,seo,http-status-code-301,.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 ^([^/]+)/([^/]+)/([^

这是我当前的.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]
基本上,它最多需要五个“友好url文件夹”,将值分配给变量,然后将它们发送到我的index.php页面

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

$p1 = 'ford';
$p2 = 'focus';
$p3 = 'grey';
$p3 = 'grey';
到目前为止,一切顺利

现在,我需要在同一个.htaccess中集成301指令(RegExp?),因为最初,我得到如下参数:

IE: http://www.example.com/ford/focus/grey/?lang=fr
我需要去掉所有get变量,因为Google将其视为重复内容(即使我在语言链接上使用了nofollow属性)

从逻辑上讲,指令应该在第一个和第二个块之间进行解释,但我不知道从哪里开始。有什么提示吗


谢谢

据我所知,您希望删除查询字符串并重定向(301永久重定向)到相同的URL,但不包含查询字符串

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

RewriteCond %{QUERY_STRING} !^$
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1? [R=301,L]
一,。
将发挥神奇的作用——将剥离查询字符串

二,。您迫切需要这一行:
RewriteCond%{ENV:REDIRECT_STATUS}^$
。问题是,它可能无法在您的Apache设置上工作,我无法提供使其工作所需的确切信息(它可以在Windows上的香草Apache v2.2.17上正常工作)

重写(内部重定向)发生后,它进入下一个迭代,Apache再次开始匹配顶部的所有规则,但对于已经重写的URL。如果我们不添加上述行,那么mod_rewrite将把上述规则应用于重写的URL表单,您将得到allURL被重写为
/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]
借助于
#不要对现有文件和文件夹执行任何操作
规则,在URL将被重写为
/index.php?p1=…
格式后,mod#u rewrite将停止重写

如果上述内容根本不起作用(更好的是——除了上述内容之外——我建议无论如何添加此内容),请在页面中使用


嗨,我现在有点急事。一旦我有时间,我将测试你的解决方案。谢谢我可能没有为您的解决方案提供良好的服务器设置。至少,我会采取你的解决方案的每一部分,并尝试搜索它,我很肯定我会找到一些东西。再次感谢@Jivago什么对你不起作用?其他规则是否有效(我的意思是——是否启用了mod_重写)?
# 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]