SOに関するこの質問のきれいなURLを注意深く見て、いくつかのアイデアを得ることをお勧めします。言うまでもなく、SOの質問はGoogleの検索結果で非常に上位にランクされます。したがって、SO URLの規則からヒントを得て、次の3つのきれいなURL形式をお勧めします。
- / articles / 89 / mytitle for
/articles_en.php?artid=89
- / halls / 65 / sometitle for
/halls.php?fairid=65
- / Companies / 65-23 / company for
/companies.php?fairid=65&hallid=23
次に、3つのルックアップテーブルarticles
を作成し、次halls
のcompanies
ようにします。
表:記事:
+-------+-------+
| artid | title |
+-------+-------+
テーブルホール:
+--------+-------+
| fairid | title |
+--------+-------+
テーブル会社:
+--------+--------+------+
| fairid | hallid | name |
+--------+--------+------+
上記の3つのきれいなURL処理では、このコードを.htaccessの次の場所に追加します$DOCUMENT_ROOT
。
RewriteCond %{QUERY_STRING} ^artid=\d+$ [NC]
RewriteRule ^articles_en\.php/?$ router.php?handler=article [L,NC,QSA]
RewriteRule ^articles/(\d+)/?(.*)$ router.php?handler=article&artid=$1&title=$2 [L,NC,QSA]
RewriteCond %{QUERY_STRING} ^fairid=\d+$ [NC]
RewriteRule ^halls\.php/?$ router.php?handler=hall [L,NC,QSA]
RewriteRule ^halls/(\d+)/?(.*)$ router.php?handler=hall&fairid=$1&title=$2 [L,NC,QSA]
RewriteCond %{QUERY_STRING} ^fairid=\d+&hallid=\d+$ [NC]
RewriteRule ^companies\.php/?$ router.php?handler=company [L,NC,QSA]
RewriteRule ^companies/(\d+)-(\d+)/?(.*)$ router.php?handler=company&fairid=$1&hallid=$2&name=$3 [L,NC,QSA]
最後に、次のrouter.php
ようなコードを作成します:(サンプルコード)
<?php
// TODO: Add sanitization checks for presence of required parameters e.g. handler and lookup failures
$handler = mysql_real_escape_string($_GET['handler']);
switch ($handler) {
case 'article':
$artid = mysql_real_escape_string($_GET['artid']);
$title = mysql_real_escape_string($_GET['title']);
if (empty($title)) {
#header("HTTP/1.1 301 Moved Permanently");
header("Location: /articles/$artid/" . lookupArticle($artid));
exit;
}
else
require_once("articles_en.php");
break;
case 'hall':
$fairid = mysql_real_escape_string($_GET['fairid']);
$title = mysql_real_escape_string($_GET['title']);
if (empty($title)) {
#header("HTTP/1.1 301 Moved Permanently");
header("Location: /halls/$fairid/" . lookupHall($fairid));
exit;
}
else
require_once("halls.php");
break;
case 'company':
$fairid = mysql_real_escape_string($_GET['fairid']);
$hallid = mysql_real_escape_string($_GET['hallid']);
$name = mysql_real_escape_string($_GET['name']);
if (empty($name)) {
#header("HTTP/1.1 301 Moved Permanently");
header("Location: /companies/$fairid-$hallid/" . lookupCompany($fairid, $hallid));
exit;
}
else
require_once("companies.php");
break;
}
function lookupArticle($artid) {
// $title = mysql_result(mysql_query("SELECT title FROM articles WHERE artid=$artid"), 0, "title");
static $articles = array(89 => 'Title\'s A', 90 => 'Title, 1B', 91 => '@Article= C');
return normalize($articles[$artid]);
}
function lookupHall($fairid) {
// $title = mysql_result(mysql_query("SELECT title FROM halls WHERE fairid=$fairid"), 0, "title");
static $halls = array(65 => 'Hall+ A', 66 => 'Hall B', 67=> 'Hall C');
return normalize($halls[$fairid]);
}
function lookupCompany($fairid, $hallid) {
// $title = mysql_result(mysql_query("SELECT name FROM companies WHERE fairid=$fairid and hallid=$hallid"), 0, "name");
static $companies = array('65-23' => 'Company% A', '66-24' => 'Company B', '67-25' => '(Company) C');
return normalize($companies[$fairid .'-'. $hallid]);
}
function normalize($str) {
return preg_replace(array('#[^\pL\d\s]+#', '#\s+#'), array('', '-'), strtolower($str));
}
?>
それがうまく機能していることを確認し301 Moved Permanently
たら、より良いSEO結果を得るためにコメントを外します。
PS: PHP関数を使用normalize
して、すべてのURLテキストを小文字で取得し、特殊文字をクリーンアップして、すべてのスペースをハイフンに変換しました。