2

既存の Web サイトを再構築し、同時に URL をよりユーザーフレンドリーな形式に書き直そうとしています。私が今持っているものhttp://example.com/test_vars.php?test_var1=value1&test_var2=value2と、理想的には、私が欲しいものはhttp://example.com/test_vars/test_var1/value1/test_var2/value2

確かに、私は htaccess ファイルを変更するのは初めてですが、調査を行い、いくつかのことを試しました。私が探しているものに最も近いもの(確かにそれほど近くはありません)ですが、以下のコードを同封しました:

Options +FollowSymLinks
Options +Indexes
RewriteEngine on
DirectoryIndex index.html

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(([^/]+/)*[^.]+)$ /$1 [L]

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.html
RewriteRule .* index.html

これで、最終的に次の URL になりますhttp://example.com/test_vars.php/test_var1/value1/test_var2/value2が、どの変数もページに渡されていません。

4

1 に答える 1

1

ではtest_vars.php、パラメータを手動で解析する必要があります。には$_SERVER['REQUEST_URI']が含まれます/test_var1/value1/test_var2/value2explode()たとえば、文字列を解析する単純な関数を作成します。

URL に「.php」拡張子を付けないようにするには、この行を変更します。

RewriteRule ^(([^/]+/)*[^.]+)$ /$1 [L]

これに:

RewriteRule ^test_vars test_vars.php

これできれいな URL ができました: http://example.com/test_vars/test_var1/value1/test_var2/value2

于 2012-05-21T23:58:44.530 に答える