0

この .htaccess ファイルの書き換えについて助けが必要です。これは私が今持っているもので、これは機能しますが、新しい RewriteRule を追加しようとしても何も起こりません。書き換えたいURLはindex.php?page=$1

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1

だから私がそれをするとき:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1

次のようにすると、ページにはcssがありません。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*_)$ index.php?page=$1

ページには css がありますが、まだ index.php?page=pagetitle を取得しています。しかし、プロフィールページには / usernameが表示されます。

4

2 に答える 2

0

書き換えルールは正規表現に基づいているため、サーバーが使用する 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
于 2013-11-09T16:07:26.280 に答える
0
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1

サーバーにすべての URL を 2 つの異なるページにリダイレクトするように要求していますが、サーバーはどのページを読み込むかを推測するだけでは機能しません。

必要なのは、/profile/username ルールまたは /page/pagetitle ルールのいずれかです。IEのようなもの:

RewriteRule ^profile/(.*)$ profile.php?username=$1 [QSA]
RewriteRule ^(.*)$ index.php?page=$1 [L]
于 2013-11-09T13:46:43.483 に答える