【问题标题】:URL Rewriting for url with countrycode带有国家代码的 url 的 URL 重写
【发布时间】:2011-07-23 05:43:26
【问题描述】:

我正在编写一个多语言网站。因此,我需要一些有关 URL 重写问题的帮助。

案例:

  • 当有人访问 www.example.com 而不添加国家/地区代码(nl、en、de)时,htaccess 会将访问者重定向到 www.example.com/nl/ i.g. RewriteRule !(nl|en|de)(.*).* /nl/ [R=301,L]
  • 该网站已更新,并且有许多指向该网站(谷歌、论坛)的 url。例如www.example.com/oldpage-nomore.html。我想要的是以下内容; htaccess 应该检测到请求 uri 不包含 nl、en 或 de,并且应该重定向到 pagenotfound.php。 RewriteRule ^(.*)\.html /public/oldurl?section=nl&notfound=$1$2&basehref=true&%1 [PT,L] 这个重写规则的问题是:所有以 .html 结尾的文件都被重定向了。

我正在寻找的是以下内容:

  • 当有人访问 www.example.com 并且没有输入请求 uri 时,这应该重定向到 www.example.com/nl/
  • 当请求的 uri 不包含国家代码 (nl|en|de) 时,重定向到 pagenotfound.php

我尝试了以下方法,但它不起作用:

RewriteCond %{REQUEST_URI} !^(nl|en|de)$
RewriteRule ^([a-z]{2})/(.*)\.html$ /pagenotfound.php?page=$2 [L,R=404]

我希望有人可以提供帮助。 提前谢谢你。

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    尝试将此 .htaccess 文件放在服务器根文件夹中:

    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} ^/?$
    RewriteRule ^.*$ /nl/ [R]
    
    
    RewriteCond %{REQUEST_URI} !^/(nl|en|de)
    RewriteRule ^.*/(.*)\.html$ /pagenotfound.php?page=$2 [L,R=404]
    

    (你可能犯了几个错误,我试图修复它们) 我不确定第一个 RewriteCond %{REQUEST_URI} 正则表达式 - 也许删除问号或斜线,我现在不知道......现在无法测试。

    【讨论】:

    • 感谢您的回复。今晚我会检查一下并告诉你。
    猜你喜欢
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 2016-08-14
    • 2016-11-19
    • 2020-04-29
    相关资源
    最近更新 更多