htaccess 301 重定向查找和替换

发布于 2024-12-29 16:10:05 字数 259 浏览 3 评论 0原文

我有一个网站,涉及印度各个城市。代码从外部源创建链接,可能将一个城市命名为加尔各答、加尔各答、加尔各答等。

我需要一个 htaccess 301 重定向规则,考虑到所有拼写错误,该规则将重定向以下已知的良好拼写(加尔各答):

Calcutta-to -Delhi.html 到 Kolkata-to-Delhi.html

以及

Calcuta-Airport.html 到 Kolkata-Airport.html

谢谢。

I have a website which refers to to various Indian cities. Code makes links from an external source, which may name a city as Calcutta, calcuta, Kolkata, etc.

I need a htaccess 301 redirect rule which, given all the misspellings would redirect the following the known good spelling (Kolkata):

Calcutta-to-Delhi.html to Kolkata-to-Delhi.html

and also

Calcuta-Airport.html to Kolkata-Airport.html

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自找没趣 2025-01-05 16:10:05

你所要求的并不简单,需要你拥有一台功能强大的计算机,但结果却是令人惊叹的。

我建议这样做:

  • 您必须测试您的表达式是否与 URL / 匹配,即 http://mysite.com/(expr1)-to-(expr2)/
  • 如果是并且没有文件,则重定向到处理所有内容的 Php 文件。
  • 在此 Php 文件中,分析 URL(再次):如果您的表达式与 /(expr1)-to-(expr2)/ 匹配,则执行 SOUNDEX 使用 MySQL 进行搜索(在您的 Php 文件中)。请参阅此处的查询示例。
  • 然后,在这种“特殊”404 情况下,像谷歌那样提出建议,即:“您的意思是 Kolkata-to-Delhi.html?如果是这样,请单击链接”。

这是一项艰苦的工作,但既有趣又能展示你的技能。很少有网站这样做(我实际上只知道谷歌)。

What you're asking for is not simple and need you to have a powerful computer, but the results are simply amazing.

Here's what I'd suggest to do:

  • You have to test if your expression matches in the URL / i.e. http://mysite.com/(expr1)-to-(expr2)/
  • If yes and there's no file, redirect to a Php file that handles everything.
  • In this Php file, analyze the URL (once again): if your expression matches /(expr1)-to-(expr2)/ then do a SOUNDEX search with MySQL (in your Php file). See query sample here.
  • Then, in this "special" 404 case, do a suggestion, like google does, i.e.: "did you mean Kolkata-to-Delhi.html? if so, click on the link".

This a hard work, but it's both interesting and shows your skill. Very few websites do this (I just know google actually).

娜些时光,永不杰束 2025-01-05 16:10:05

也许你可以尝试这样的事情:

# REDIRECTS FROM (eg): http://www.calcutta.com.au/ to http://www.kolkata.com.au/
RewriteCond %{HTTP_HOST} ^calcutta\.com\.au$ [nocase, ornext]
RewriteCond %{HTTP_HOST} ^www\.calcutta\.com\.au$ [nocase]
RewriteRule ^(.*)$ http://www.kolkata.com.au/ [nocase,L,R=301,O]

注意“[nocase]”,它忽略大小写。这只会从“加尔各答”重定向到“加尔各答”,而不是从“加尔各答”或“加尔各答”重定向(请注意加尔各答中缺少“t”,加尔各答中缺少大写“K”)。

您将需要按照与上面相同的代码模式添加另外两个规则。

Maybe you can try something like this:

# REDIRECTS FROM (eg): http://www.calcutta.com.au/ to http://www.kolkata.com.au/
RewriteCond %{HTTP_HOST} ^calcutta\.com\.au$ [nocase, ornext]
RewriteCond %{HTTP_HOST} ^www\.calcutta\.com\.au$ [nocase]
RewriteRule ^(.*)$ http://www.kolkata.com.au/ [nocase,L,R=301,O]

Notice the "[nocase]" which ignores case. This will only redirect from "calcutta" to "kolkata" and not from "calcuta" or "Kolkata" (notice the missing 't' in calcuta and uppercase 'K' in Kolkata).

You will need to add two more rules following the same pattern of code as above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文