【问题标题】:How to rewrite SEO friendly url's like stackoverflow如何重写 SEO 友好的 url,如 stackoverflow
【发布时间】:2012-02-28 09:57:44
【问题描述】:

如果您访问如下网址:http://stackoverflow.com/questions/9155602/,地址栏将更新为http://stackoverflow.com/questions/9155602/redirect-existing-file-to-different-url-using-mod-rewrite。这不是用 JavaScript 完成的,也不是用硬刷新完成的,我最好的猜测是它是用 mod_rewrite 完成的;但是,如果不使用php进行服务器端处理,它怎么知道文章的标题?

我刚刚更新了我的网站以使用 SEO 友好的 url,它曾经是 /shows.php?id=review-1,现在是 /review/1/super-baseball-2020。最后的文字实际上并不重要,因为我的重写是:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([0-9]+)/([a-z0-9-]+)?/?$ /shows.php?id=$1-$2 [L,NC]

我目前确保 Google 不会因为重复内容而打击我并且书签被正确转发的路径如下:

RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-1(?:&|$) [NC]
RewriteRule ^ /review/1/super-baseball-2020/? [R=301,L,NC]
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-2(?:&|$) [NC]
RewriteRule ^ /review/2/aero-fighters-2/? [R=301,L,NC]
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-3(?:&|$) [NC]
RewriteRule ^ /review/3/aero-fighters-3/? [R=301,L,NC]
.... and so on ....

该解决方案非常短视,需要在我的 .htaccess 中放入数千行代码。

【问题讨论】:

  • 我怀疑这个功能是在 SO 上使用 mod_rewrite 完成的。发生的情况是,对 stackoverflow.com/*anything* 的任何请求都由相同的服务器端代码处理,然后解析该 URL 并将您的请求路由到为页面内容提供服务的代码使用问题 ID。问题标题被服务器忽略,尝试访问stackoverflow.com/questions/9168364/bouncing-fluffy-muffins
  • 通常 mvc 应用程序会将所有传入请求路由到同一个脚本(前端控制器),该脚本将解析请求本身,并将操作委托给适当的控制器类。

标签: php .htaccess mod-rewrite redirect url-rewriting


【解决方案1】:

没有服务器端怎么知道文章的标题 用php处理?

mod_rewrite 只能做这么多。另外,您必须对其进行太多更新。最好使用脚本处理这些请求。创建一个.htaccess,类似于您所做的:

<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [QSA,L]
</IfModule>

然后在您的index.php 中,拆分REQUEST_URI 以解析出/review/1/,在数据库中查找id 为1 的评论标题,将其传递给您的友好URI(slug)函数,并将其附加到 url,然后重定向。

【讨论】:

    【解决方案2】:

    我还为我们的节目创建了一个对 SEO 友好的 URL 解决方案。 URL自然是/raido/episode.php?SeriesId=1&EpisodeId=123,但我也想要/radio/1-123-live.htm,所以我的.htaccess是这样的:

    RewriteRule ^\d{1,3}-\d{1,4}(.+).html$ episode.php?Slug=$0

    然后我让目标页面处理它

    if ($Slug !== "") {
      $lIds = explode("-", $Slug);
      $SeriesId=$lId[0];
      $EpisodeId=$lIds[1];
    }
    

    这还允许您继续接受现有链接,而不必拥有相同代码的 2 个版本。

    【讨论】:

      【解决方案3】:

      对于重复的内容,您应该在头部有一个规范的链接标签。规范链接标签会告诉谷歌当前页面有一个与规范链接相同内容的备用网址,而该内容的主要网址是规范链接。这对于分页内容、搜索等内容非常有用,因为同一页面可能有不同的 url。

      <link rel="canonical" href="http://www.somedomain.com/mainurl" />
      

      【讨论】:

        【解决方案4】:

        如果我这样做

        curl -I http://stackoverflow.com/questions/9155602/
        

        我实际上得到了一个硬重定向:

        HTTP/1.1 301 Moved Permanently
        Cache-Control: public, max-age=60
        Content-Length: 0
        Content-Type: text/html
        Expires: Mon, 06 Feb 2012 22:43:27 GMT
        Last-Modified: Mon, 06 Feb 2012 22:42:27 GMT
        Location: /questions/9155602/redirect-existing-file-to-different-url-using-mod-rewrite
        Vary: *
        Date: Mon, 06 Feb 2012 22:42:27 GMT
        

        这些设置通常包含一个简短的 .htaccess,强制所有请求通过前端控制器或主 index.php 文件。此文件检查 $_SERVER['PATH_INFO'] 变量并决定要拉入和呈现的内容。

        这是一个简单的 .htaccess 示例(实际上只是从原版 wordpress 安装复制而来):

        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
        

        在这个例子中,使用硬重定向,他们可以在 index.php 中有一个“else”,如果根据最后一条 PATH_INFO 找不到内容,则可以正确查找和重定向。

        【讨论】:

        • 当我尝试在我知道使用 301 的 url 上卷曲时,它只会显示 200。defunctgames.com/shows.php?id=review-1。代码:RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\? RewriteCond %{QUERY_STRING} (^|&amp;)id=review-[0-9]+(&amp;|$) [NC] RewriteRule ^ /controllers/redirectseo.php? [R=301,L,NC,QSA]
        【解决方案5】:

        您将需要在应用程序本身中通过检查 URL 与请求位置的绝对 URL 来执行此操作,或者在每次添加新“页面”时通过 generating the mapping 执行此操作。

        【讨论】:

          猜你喜欢
          • 2015-07-30
          • 2010-12-12
          • 2018-07-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-24
          • 1970-01-01
          • 2021-07-06
          • 2015-01-04
          相关资源
          最近更新 更多