【问题标题】:How to rewrite 301 the old ugly urls to the new beautified working urls?如何将 301 旧的丑陋 url 重写为新的美化工作 url?
【发布时间】:2011-07-09 05:59:51
【问题描述】:

最近几个月让我很头疼的一个小问题。首先是好消息:在 massiv SO 社区的帮助下,我能够将丑陋的 url 改写成漂亮的:例如website.com/page.ast?ln=nl 转为website.com/nl/page

但是

Google 在他们的搜索中显示丑陋的网址:

website.com/nl/page.ast?ln=fa

我的网站页面代码确实有规范设置:

<link rel="canonical" href="http://website.com/nl/page">
<meta name="original-source" content="http://website.com/nl/page"/>

但是,结果显然仍然被错误的、丑陋的网址编入索引!
Q1。为什么google会忽略规范的更漂亮的链接?


也许最好以机械方式执行此操作:将那些 website.com/nl/webpage.ext?ln=yy 重定向到 website.com/nl/webpage 基本上删除了不做任何事情的不必要部分 .XXX?ln=YYYY (其中 XXX 是 2 或 3 字符扩展名, YYY 是语言(可以是nlbefr,也可以是zh-CN等)

EXAMPLE OF UGLY OLD FILES   THEIR NICE BEAUTIFIED URLS
/en/somepage.ast?ln=fr          /en/somepage
/fr/home.php?ln=zh-CN           /fr/home
/xx/zzzzzz.ext?ln=yyyy          /xx/zzzzzzz
/xx/zzzzzz?ln=yyyy              /xx/zzzzzzz
/xx/zzzzzz.ext                  /xx/zzzzzzz

第二季度。如何一次将所有丑陋的文件重写为漂亮的 url? 到目前为止,初步设计

RewriteRule   /XX/ZZZZ(.*anything after and including the dot)  /XX/ZZZZ [R=301]

那么,你有什么推荐的?我认为建议是可能的答案。非常感谢!

【问题讨论】:

    标签: apache url-rewriting apache2


    【解决方案1】:
    1. Canonical url 说的是,代表这个内容的主要 url 是不同的。对于谷歌来说,它仍然是一个不同的 URL,它会给规范 url 一些权重,但可能不会删除旧 url 并防止您考虑重复内容。 当您有添加一些与内容相关的查询参数的 url 时,它特别使用。 http://something.com/1234/234?ref=g1http://something.com/1234/234?ref=widardnb 对您来说是相同的 url,但 google 会将其视为不同的 url,现在在这种情况下您提供规范的 url http://something.com/1234/234。这是您,因为您无法控制诸如会员之类的人可能附加到您的网址的内容。

    2. 如果您实际上只想向用户永久显示美化的 url。然后你必须做301重定向。

    要做到这一点,请在 apache 配置文件中使用 mod_rewrite。

    LoadModule rewrite_module /path/to/modules/dir/mod_rewrite.so
    
    RewriteEngine On
    
    RewriteBase /
    RewriteCond %{QUERY_STRING} ~ !^$ [OR]   #Query string is not empty
    RewriteCond %{REQUEST_URI} ~ ^(.*)\.(.*)$ #Your url path have . in between
    RewriteRule .*  %1 [R=301,L]  #then redirect to path without extension (else all are handled as it is).
    

    同时启用重写日志

    RewriteLog "/path/to/log/directory/rewrite.log" #change the directory name
    RewriteLogLevel 9  #this will help you understanding whats happening behind (disable this once everything starts working) 
    

    我没有测试过,但如果有任何错误,请告诉我。

    【讨论】:

    • 非常有趣。我几乎不敢相信 Rewrite Engine On 下面的 4 个 lnes 可以解决我的问题 :) 你能在// comment style 的每个 coer 之后写下每一行的作用吗?然后我将对其进行测试并在这里报告。到目前为止非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    相关资源
    最近更新 更多