【问题标题】:htaccess 301 redirect old page to new page dynamiclyhtaccess 301 将旧页面动态重定向到新页面
【发布时间】:2012-10-11 19:09:29
【问题描述】:

我已经更改了我的地址并将它们设置为 sef,但我的一些链接是旧的,其中一些是由人们共享的,如果他们尝试进入那里,他们会看到 500 internal server error,正如你想象的那样,我需要将它们重定向到新的 url,我知道如何静态重定向,但不知道如何动态重定向

例如,我的旧链接是:mywebsite.com/profile/1,我的新链接是 mywebsite.com/profile/1/johnny-walker

如果有人试图查看我的旧链接,我希望他们重定向到我的新链接

我的旧规则是:

RewriteRule ^profile/(.*)/?$ profile.php?id=$1 [L] 

我的新规则是:

RewriteRule ^profile/([0-9]*)/(.*)/?$  profile.php?id=$1&slug=$2 [QSA,L]

谢谢

【问题讨论】:

  • “johnny-walker”从何而来?
  • 那是那个人的名字,它来自数据库,我用get exc.调用它,当用户注册时,它是自动创建的,我只是写它作为例子

标签: .htaccess redirect http-status-code-301


【解决方案1】:

这是那个人的名字,它来自数据库,我用get exc.调用它,当用户注册时,它是自动创建的,我只是写它作为示例

我的意思是,在你拥有的只是1(id)之前。从那个id,你怎么得到蛞蝓?如果可以从数据库中获取,则需要同时保留两条规则(稍作改动):

RewriteRule ^profile/([0-9]+)/([^/]+)/? profile.php?id=$1&slug=$2 [QSA,L]
RewriteRule ^profile/([0-9]+)/? profile.php?id=$1 [L] 

那么在profile.php你需要做以下事情:

  1. 在文件的开头,检查是否有$_GET['slug'] 值,如果有,像正常一样打开页面
  2. 如果没有 slug 值,那么您需要获取 $_GET['id'](已清理)值并从数据库中检索 slug
  3. 使用header() 函数,创建重定向:

    header('HTTP/1.1 301 Moved Permanently');
    header('Location: /profile/' . $id . '/' . $slug);
    exit();
    

    $id 是 id,$slug 是数据库中的 slug 值。

这样当有人请求 http://mywebsite.com/profile/1 时,重写规则会将其发送到 profile.php,就像之前可以制作规范 URL 并进行重定向一样。

【讨论】:

    猜你喜欢
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2012-03-21
    • 2022-01-22
    • 1970-01-01
    • 2012-05-15
    • 2013-02-10
    相关资源
    最近更新 更多