書き換えルールは正規表現に基づいているため、サーバーが使用する URL を正確に判断できるように、できるだけ具体的にする必要があります。 ? URL で「user」、「profile」などのプレフィックスを使用すると、http://example.com/profile/somethingをユーザー名としてリダイレクトし、他のすべてのデフォルト リダイレクトを使用できることを意味します。これを実現するには、より具体的なパターン マッチを最初に作成し (ユーザー)、[L]
ディレクティブを使用して、次のルールを処理しないことを示す必要があります。私は通常、スラッシュ以外のものと一致するように URL に否定文字クラスを使用します- [^/]*
.
# Enable mod_rewrite
RewriteEngine On
# Set the base directory
RewriteBase /
# Don't process if this is an actual file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Does this url start with /profile and then followed with additional characters?
RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]
# Assume everything else is a page
RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
http://htaccess.madewithlove.be/でテストします (%{REQUEST_FILENAME}
と%{REQUEST_FILENAME}
はテスト用にサポートされていないことに注意してください)。
プロフィール
input url
http://www.example.com/profile/something
output url
http://www.example.com/profile.php
debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,QSA,L]
This rule was met, the new url is http://www.example.com/profile.php
The tests are stopped because the L in your RewriteRule options
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
ページ
input url
http://www.example.com/something
output url
http://www.example.com/index.php
debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
This rule was met, the new url is http://www.example.com/index.php
The tests are stopped because the L in your RewriteRule options