2012-08-26 58 views
0

我想用htaccess重定向很多页面(150个链接),但是我想问一下是否可以有重写规则。用htaccess重定向很多页面

我的旧链接是;

http://www.sitename.com/aaa/category/article.html

http://www.sitename.com/aaa/category/differentarticle.html

http://www.sitename.com/aaa/anothercategory/anotherarticle.html

http://www.sitename.com/aaa/anothercategory/anotherarticletoo.html

新链接

http://www.sitename.com/aaa/111-category/999-article.html

http://www.sitename.com/aaa/111-category/990-differentarticle.html

http://www.sitename.com/aaa/222-anothercategory/888-article.html

http://www.sitename.com/aaa/222-anothercategory/886-anotherarticletoo.html

的问题是,有很多的文章,他们都有不同的文章编号。

我该如何为每个类别编写301重定向规则?

谢谢..

+0

而且您希望将旧链接设为新的,以便拥有书签的用户仍然可以找到该页面?或其他方式,使它看起来不同,但服务器仍然处理它一样? –

+0

所有这些ID从哪里来?如果他们来自数据库,你打算如何让htaccess文件与数据库交谈? –

+0

这是一个joomla 1.5网站,我想改变菜单项(单篇文章)到分类文章链接的链接。但是这些链接(旧)在域中使用。域拥有超过10000个网页。好吧,我认为我不能这样做,如果我添加200重定向规则到htaccess它会减慢网站? –

回答

1

我会假设你想重写http://www.sitename.com/aaa/category/article.htmlhttp://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 
?> 

你应该还添加了更多的错误检查功能,我将它保留了很短的时间