まず、URL を定義する必要があります!!!
お気に入り:
/blog
ショーfront page
/blog/1234
ショーpost 1234
/blog/date/2012
ショーposts by year
/blog/date/2012/06
ショーposts by year and month
/blog/date/2012/06/01
ショーposts by year and month and day
等々...
最初のオプションは、定義した各 URL を index.php に書き換えることです。index.php は、送信された GET パラメータのみを処理する必要があります。
### Do only if rewrite is installed
<IfModule mod_rewrite.c>
### Start rewrite and set basedir
RewriteEngine on
RewriteBase /
### Rewrite only if no file link or dir exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
### Rewrite frontpage
RewriteRule ^blog$ /index.php?action=showfront [L,QSA]
### Rewrite post
RewriteRule ^blog/([0-9]+)$ /index.php?action=showpost_by_id&id=$1 [L,QSA]
### Rewrite posts by date
RewriteRule ^blog/date/([0-9]{4})$ /index.php?action=showposts_by_date&year=$1 [L,QSA]
RewriteRule ^blog/date/([0-9]{4})/([0-9]{2})$ /index.php?action=showposts_by_date&year=$1&month=$2 [L,QSA]
RewriteRule ^blog/date/([0-9]{4})/([0-9]{2})/([0-9]{2})$ /index.php?action=showposts_by_date&year=$1&month=$2&day=$3 [L,QSA]
### Rewrite posts by tag
RewriteRule ^blog/tag/([a-zA-Z0-9_-]+)$ /index.php?action=showposts_by_tag&tag=$1 [L,QSA]
</IfModule>
次のように index.php でテストします: print_r($_GET); print_r($_POST);
2 番目のオプションは、すべての URL を書き換えることであり、index.php は考えられるすべての URL を処理する必要があります。そのため、最初は受信 URL を部分的に分割し、要求されたページまたはエラー ページを送信するルーターのようなものが必要です。私は血まみれの学校として最初にこれを試してみます.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php%{REQUEST_URI} [L]
</IfModule>
次のように index.php でテストします。
print_r(explode('/', ltrim($_SERVER['PATH_INFO'], '/')));
print_r($_GET);
print_r($_POST);
3 番目のオプションは、PHP フレームワークを使用することです。フレームワークは、コードを非常に速く書くのに役立つ場合があります。ルーターのような多くの基本クラスを提供します。(fe ZendFramework、Flow3、Kohana、Symfony、CodeIgniter、CakePHP、yii など)。これにより、より高度になります。
4番目の最も怠惰なオプションは、Wordpressのような既製のソフトウェアを使用することです.