0

Webサイトに2つの異なるルールを設定しようとして.htaccessいますが、それでも正しい解決策が見つかりません。

私はすべてをルーティングしたいと思いますwebsite.com/almost-everything-これは私をうまく機能させています。さらに、このルートを追加したいと思います。-そしてwebsite.com/car/car_id、ここで問題が発生します。設定方法がわかりません。

これが私の試みです:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?skill=$1 [L,QSA]
RewriteRule ^car/(.*)$ ./index.php?id=car&car_id=$1 # the wrong rule - the page with website.com/car/car_id just doesn't display the correct file

2番目のルールを教えていただけませんか。

4

3 に答える 3

1

上から下に、1行ずつ書き直します。

初期条件(ファイルが存在しない)を確認した後、最初のルールに遭遇します。

URLが何かである場合は、それを変更すると書かれています。また、2つのオプションがあります。

  • 「QSA」は、クエリ文字列を追加することを意味します
  • 「L」はこれが最後のルールであることを意味するため、処理を停止します

この「L」により、処理が停止し、このルールの後には何も起こりません。

これを修正するには:

  • 「car/」はより具体的であるため、ルールの順序を変更します
  • また、LフラグとQSAフラグを「car/」ルールに追加します。

それで:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^car/(.*)$ ./index.php?id=car&car_id=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?skill=$1 [L,QSA]
于 2013-01-17T18:10:04.773 に答える
1

それ以外の

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?skill=$1 [L,QSA]
    RewriteRule ^car/(.*)$ ./index.php?id=car&car_id=$1 # the wrong rule - the page with website.com/car/car_id just doesn't display the correct file

私はこれをします

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]   ## passthru + last rule because the file or directory exists. And stop all other rewrites. This will also help your css and images work properly.

RewriteRule ^car/(.*)$  /index\.php?id=car&car_id=$1 [L,QSA]

RewriteRule ^(.*)$  /index\.php?skill=$1 [L,QSA]

追伸私はルールを空白行で区切ったので、いくつあるかは明らかです。上記は3つの異なるルールを示しています。

于 2013-01-17T18:43:51.420 に答える
0

より良い解決策は、すべてのリクエストをにリダイレクトしてからindex.php、解析すること$_SERVER['REQUEST_URI']です。次に、新しい未来ごとにhtaccessを変更する必要があります。

apacheでは、このように行うことができます>

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [L]

PHPでは、手動で入力できるため$_GET、古いリクエストのように見えます...

$f = explode('/', substr($_SERVER['REQUEST_URI'], 1));
switch ($f[0]) {
    case 'car' :
        $_GET['id'] = $f[0];
        $_GET['car_id'] = $f[1];
        break;
    default:
        $_GET['skill'] = $f[0];
}

# your old code, that reads info from $_GET

より良い方法は、URLを処理するクラスを作成することです。

于 2013-01-17T18:13:53.460 に答える