http://www.sitename.com/aaa/category/article.htmlをhttp://www.sitename.com/aaa/111-category/999-article.htmlに書き換えたいと仮定します。 、および記事 (少なくとも名前と ID) を格納するために PHP と何らかの形式のデータベースを使用していること
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
?>
また、エラーチェックを追加する必要があります。短くするために省略しました