0

私の試み:

RewriteCond %{QUERY_STRING}     ^id=(.*)$    [NC]
RewriteRule ^/product$       /product/%1      [NC,L,R=301]

/product/このルールを/supplier/ディレクトリのみに適用したい。どちらも第 1 レベルのサブディレクトリです。

注:product/?id={xxx}実際にはproduct/index.php?id={xxx}. Apache は拡張機能とインデックスを隠します。それを指摘したいだけです。

Myproduct/index.phpは指定されたパラメーターを処理し、表示するページを決定します。

index.php

if ( isset( $_GET['id'] ) && !empty( $_GET['id'] ) ) {
   //html for individual page e.g. /product/?id=foo
   //e.g. <h1><?= $_GET['id'] ?> Page</h1>
} else {
   //html for product list e.g. /product/ (no parameters)
}
4

1 に答える 1

1

ルート ディレクトリにある .htaccess ファイルでこれを試してください。

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} id=(.+)          [NC]
RewriteRule ^(product|supplier)/?$   /$1/%1? [NC,L,R=301]

.htaccess ファイルでは、ルールの URI パス テストに先頭のスラッシュ ( ^/product) がないため、正規表現にも含めることができません。末尾?は受信クエリを削除します。

ルール セットを Apache のメイン構成ファイルに配置する場合は、先頭のスラッシュを保持する必要があります。^/(product|supplier)/?$

アップデート

目的の URL を表示しながら、元の URL からデータを取得するには。

リクエスト:/product/?id=parameter

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/(product|supplier)/\?id=([^\s]+) [NC]
# Strip the query and redirect permanently
RewriteRule  ^(product|supplier)  /$1/%3?   [R=301,L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^$
# Map internally to the original request
RewriteRule  ^(product|supplier)/([^/]+)/?  /$1/?id=$2  [L,NC]

別のオプションは、リクエストで「pretty」URL を直接使用することです。

/product/parameterへのリクエスト/product/?id=parameter

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^$
RewriteRule  ^(product|supplier)/([^/]+)/?  /$1/?id=$2  [L,NC]
于 2013-04-18T20:48:39.413 に答える