0

htaccess を使用して URL を書き換えています 元の URL は

http://www.example.com/products/product_detail.php?url=pro-name

私の希望のURLはこのようになります

http://www.example.com/products/pro-name

しかし、私はこのURLで部分的に完了しています

 http://www.example.com/products/product_detai/pro-name

この .htaccess コードを使用して

RewriteRule product_detail/url/(.*)/ product_detail.php?url=$1
RewriteRule /(.*) product_detail.php?url=$1

ここで、目的の URL を取得する方法がわかりませんでした。希望のURLを取得するために誰かを助けてください。ありがとう

4

2 に答える 2

1

mod_rewrite と .htaccess を有効にしてからhttpd.conf、このコードをディレクトリの.htaccess下に配置します。DOCUMENT_ROOT

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(products)/product_detail.php\?url=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]

RewriteRule ^(products)/(.+?)/?$ /$1/product_detail.php?url=$2 [L,NC,QSA]
于 2013-07-22T11:10:16.647 に答える
0
#Assuming the correct RewriteBase is used...

#Redirect the client to the fancy url
RewriteCond %{QUERY_STRING} ^url=(.*)$
RewriteRule ^product_detail\.php$ %1? [R,L]

#Rewrite the url internally and stop rewriting
#to prevent a loop
RewriteRule ^(.*)$ product_detail.php?url=$1 [END]

このコードでは、最初にクライアントをアドレス バーに表示する URL にリダイレクトします。%1RewriteCond の最初のキャプチャ グループに一致します。末尾?はクエリ文字列をクリアします。2 番目のルールは、サーバーが 404 エラーの代わりに実際に出力を生成できるように、URL を内部的に書き換えます。END フラグ (apache 2.3.9 以降で利用可能; docs ) は、URL の書き換えを完全に停止します)。これは、URL が絶えず書き換えられる終わりのないループを防ぐためです。(ドキュメント)

編集: 2.3.9 より前のバージョンの apache には END フラグがありません。ループを防ぐには、それを回避する必要があります。たとえば、次のように使用できます。

#Assuming the correct RewriteBase is used...

#Redirect the client to the fancy url
RewriteCond %{QUERY_STRING} !redirect=true
RewriteCond %{QUERY_STRING} ^url=(.*)$
RewriteRule ^product_detail\.php$ %1? [R,L]

#Rewrite the url internally and stop rewriting
#to prevent a loop
RewriteRule ^(.*)$ product_detail.php?url=$1&redirect=true [L]
于 2013-07-22T10:29:49.337 に答える