0

I want to redirect lots of pages (150 links) with htaccess, but i want to ask if there can be a rewrite rule.

My old links are;

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

New links are

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

The problem is that there are lots of articles and all of them have different article ids.

How can i write a rule for every category to 301 redirect?

Thanks..

John Rock
  • 129
  • 2
  • 10
  • And you want to make old links new so people with bookmarks can still find the page? or other way around so that it only looks different but the server still handles it the same? – Andrew Brock Aug 26 '12 at 01:05
  • Where do all these IDs come from? If they come from a database how do you intend to get the htaccess file to talk to the database? – Jon Lin Aug 26 '12 at 01:13
  • It's a joomla 1.5 site, i want to change the links of menu items (single article) to category article link. But these links (old) are used in the domain. Domain has 10000+ pages. Ok i think i can't do that with a rule, if i add 200 redirect rule to htaccess will it slow the site? – John Rock Aug 26 '12 at 07:07

1 Answers1

1

I'm going to assume that you want to rewrite http://www.sitename.com/aaa/category/article.html to http://www.sitename.com/aaa/111-category/999-article.html, and that you are using PHP and some form of database for storing the articles (at least the names and IDs)

ModRewirte can't add in extra data (unless it is hard coded values which would apply the same to each url).

You can, however make it do an internal rewrite (not a redirect) for everything to index.php, then index.php can read the $_SERVER['REQUEST_URI'] to see what URL is requested. This will get you /aaa/category/article.html which you can then do whatever you want with PHP, including send off a redirect.

The following are extracts from where I performed the same trick, but with a different purpose. The first bit ignores locations such as the css and images folder. Anything which does not match those locations will be sent to 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

and your index.php (or whatever you want to call it: mine is 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
?>

You should also add more error checking, I left it out to keep it short

Andrew Brock
  • 1,374
  • 8
  • 13