使用正则表达式进行 htaccess 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]
基本上,它最多需要五个“友好的 url 文件夹”并将值分配给变量,然后将它们发送到我的 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/
从逻辑上讲,应该在第一个和第二个块之间解释指令,但我只是不知道从哪里开始。有什么提示吗?
谢谢!
This is my current .htaccess file
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]
Basically, it takes up to five "Friendly url folders" and assign the value to varibles and then, send those to my index.php page
IE: http://www.example.com/ford/focus/grey/
$p1 = 'ford';
$p2 = 'focus';
$p3 = 'grey';
$p3 = 'grey';
So far, so good.
Now, I need to integrate a 301 instruction (RegExp?) in that same .htaccess because initially, I had GET parameters like this :
IE: http://www.example.com/ford/focus/grey/?lang=fr
I need to get rid of all GET variables because Google sees it as duplicate content (even if I'm using the nofollow attribute on my languages links)
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/
Logically, the instruction should be interpreted between the first and the second block but I just don't know where to start. Any hints?
THANKS!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,您希望摆脱查询字符串并将其重定向(301 永久重定向)到相同的 URL,但没有查询字符串。
下面的规则将重定向每个包含查询字符串的请求:
1.
?
会发挥作用——将删除查询字符串2. 您迫切需要这一行:
RewriteCond %{ENV:REDIRECT_STATUS } ^$
。问题是它可能无法在您的 Apache 设置上工作,而且我无法为您提供使其工作所需的确切内容(它在 Windows 上的普通 Apache v2.2.17 上工作正常)。重写(内部重定向)发生后,它会进入下一次迭代,Apache 会再次从顶部开始匹配所有规则,但对于已经重写的 URL。如果我们不添加上面的行,那么 mod_rewrite 会将上述规则应用于重写的 URL 形式,最终所有 URL 都会被重写为
/index.php
,而没有任何内容参数都没有。如果上面的方法不起作用,请尝试下面的代码:
在
# do not do everything for已经存在的文件和文件夹
规则的帮助下,mod_rewrite将在URL重写为/后停止重写index.php?p1=...
格式。如果上面的方法根本不起作用(更好 - 除了上述之外 - 我建议无论如何添加这个)使用
在您的页面中:
As I understand you want to get rid of the QUERY STRING and redirect (301 Permanent Redirect) to the same URL but without QUERY STRING.
The rule below will redirect EVERY request that has query string:
1. The
?
will do the magic -- will strip query string2. You desperately need this line:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
. The problem is that it may not work on your Apache setup and I cannot give you what exactly you need to make it work (it works fine on my vanilla Apache v2.2.17 on Windows).After rewrite (internal redirect) occurred, it goes to next iteration and Apache starts matching all rules from the top again but for already rewritten URL. If we not add the above line, then mod_rewrite will apply the above rule to rewritten URL form and you will end up with all URLs get rewritten to
/index.php
with no parameters at all.If the above will not work, then try the code below:
With help of
# do not do anything for already existing files and folders
rule, mod_rewrite will stop rewriting after URL will be rewritten to/index.php?p1=...
format.In case the above will not work at all (better -- in addition to the above -- I would suggest adding this anyway) use
<link rel="canonical" href="FULL_PROPER_RUL"/>
in your page: