简体   繁体   English

htaccess重定向301网址重写

[英]htaccess redirect 301 url rewrite

we got stuck with an url rewrite and redirection 301 in .htacces 我们陷入了.htacces中的网址重写和重定向301

At the moment our Internetsite has only got one file (index.php) which get passed as variable the Category ID to then show the content of that category. 目前,我们的Internet站点只有一个文件(index.php),该文件作为变量传递给类别ID,然后显示该类别的内容。

For example: the Category Nr 4 gets called with the link www.oursite.com/index.php?CATID=4 which shows the content of the category "map-france" the Category Nr 5 gets called with the link www.oursite.com/index.php?CATID=5 which shows the content of the category "map-italy" 例如:类别Nr 4通过链接www.oursite.com/index.php?CATID=4进行调用,该链接显示类别“ map-france”的内容,类别Nr 5通过链接www.oursite进行调用。 com / index.php?CATID = 5,它显示类别“ map-italy”的内容

Alltogether we have ca 800 Categories. 我们总共有大约800个类别。

We would like to rewrite the urls so that the category names will be displayed: For example: www.oursite.com/map-france instead of www.oursite.com/index.php?CATID=4 www.oursite.com/map-italy instead of www.oursite.com/index.php?CATID=5 我们想重写URL,以便显示类别名称:例如:www.oursite.com/map-france而不是www.oursite.com/index.php?CATID=4 www.oursite.com/map -italy而不是www.oursite.com/index.php?CATID=5

The best idea at the moment is to use the mod_rewrite functions in .htacces 目前最好的想法是在.htacces中使用mod_rewrite函数

We have managed so far to achieve this with the following code: 到目前为止,我们已经设法通过以下代码实现这一目标:

RewriteEngine On
RewriteRule    ^map-france/?$    index.php?CATID=4    [NC,L]
RewriteRule    ^map-italy/?$    index.php?CATID=5    [NC,L]

This works pefectly. 这很有效。

To avoid double indexing from search engines we should also apply a redirect 301 from the old urls to the new ones. 为了避免来自搜索引擎的双重索引,我们还应该将重定向301从旧网址重定向到新网址。 We managed to do that with the following code: 我们使用以下代码设法做到这一点:

RewriteCond %{QUERY_STRING} CATID=4
RewriteRule ^index\.php$  http://www.oursite.com/map-france/? [L,R=301]

RewriteCond %{QUERY_STRING} CATID=5
RewriteRule ^index\.php$  http://www.oursite.com/map-italy/? [L,R=301]

If we put everything together it does not work and it seems to generate a never ending redirection loop 如果我们将所有内容放在一起,它将无法正常工作,并且似乎会产生永无止境的重定向循环

What are we doing wrong? 我们做错了什么?

Thanks a lot for your help :) 非常感谢你的帮助 :)

Try matching agaist the request instead of the %{QUERY_STRING} 尝试匹配请求,而不是%{QUERY_STRING}

RewriteCond %{THE_REQUEST} \?CATID=4
RewriteRule ^index\.php$  http://www.oursite.com/map-france/? [L,R=301]

RewriteCond %{THE_REQUEST} \?CATID=5
RewriteRule ^index\.php$  http://www.oursite.com/map-italy/? [L,R=301]

The rewrite engine loops until the URI stops changing, that means your first rule adds the query string, and you second rule matches the query string and redirects to without the query string. 重写引擎将循环直到URI停止更改为止,这意味着您的第一个规则将添加查询字符串,而第二个规则将与查询字符串匹配并重定向到不包含查询字符串的位置。 Then the browser makes the same request again, and your first rule gets applied, then the second rule matches against the query string and redirects the browser again , etc. 然后浏览器再次发出相同的请求,并且您的第一个规则被应用,然后第二个规则与查询字符串匹配并再次重定向浏览器,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM