【问题标题】:Redirect Lots of Pages with htaccess使用 htaccess 重定向大量页面
【发布时间】:2012-08-26 00:57:12
【问题描述】:

【问题讨论】:

  • 您想将旧链接设为新链接,以便拥有书签的人仍能找到该页面?或其他方式,以便它看起来不同但服务器仍然处理它?
  • 所有这些 ID 是从哪里来的?如果它们来自数据库,您打算如何让 htaccess 文件与数据库通信?
  • 这是一个 joomla 1.5 站点,我想将菜单项(单篇文章)的链接更改为类别文章链接。但是这些链接(旧)在域中使用。域有 10000 多个页面。好的,我想我不能用规则来做到这一点,如果我向 htaccess 添加 200 重定向规则会减慢网站速度吗?

标签: .htaccess redirect categories article


【解决方案1】:

我将假设您想将 http://www.sitename.com/aaa/category/article.html 重写为 http://www.sitename.com/aaa/111-category/999-article.html,并且您正在使用 PHP 和某种形式的数据库来存储文章(至少是名称和 ID)

ModRewirte 不能添加额外的数据(除非它是硬编码的值,它将对每个 url 应用相同的值)。

但是,您可以让它对index.php 的所有内容进行内部重写(而不是重定向),然后 index.php 可以读取 $_SERVER['REQUEST_URI'] 以查看请求的 URL。这将为您提供/aaa/category/article.html,然后您可以使用 PHP 做任何您想做的事情,包括发送重定向。

以下是我执行相同技巧但目的不同的摘录。第一位忽略诸如 css 和 images 文件夹之类的位置。任何与这些位置不匹配的内容都将发送到 index.php

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On

RewriteCond %{REQUEST_URI} !/css/.*$
RewriteCond %{REQUEST_URI} !/images/.*$
RewriteCond %{REQUEST_URI} !/js/.*$
RewriteCond %{REQUEST_URI} !/favicon\.ico$
RewriteCond %{REQUEST_URI} !/robots\.txt$
RewriteRule . index.php

还有你的 index.php(或者你想怎么称呼它:我的是 router.php)

<?php
function http_redirect($url, $code=301) {
    header('HTTP/1.1 '.$code.' Found');
    header('Location: '.$url);
    echo 'Redirecting to <a>'.$url.'</a>.';
    die('');
}


$loc = explode('?', $_SERVER['REQUEST_URI']); //Get rid of any query string
$query = loc[1];
$loc = explode('/', $loc[0]);
//you now have
//array('aaa','category','article.html')
//look these up in the database to build your new URL
http_redirect('my/new/url'.$loc[0]); //whatever, don't forget the query string if it has 1
?>

你还应该添加更多的错误检查,为了简短起见,我把它省略了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 2023-03-04
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多