10

Apache で mod-rewrite を使用するサーバーと、Nginx で HttpRewriteModule を使用するサーバーの 2 つの別々のサーバーでパスの書き換えを設定しようとしています。複雑すぎることをしようとしているとは思いませんが、正規表現のスキルが少し不足しており、本当に助けが必要です。

具体的には、フォーマットされた URL をクエリ文字列に変換して、リンクが次のようにフォーマットされるようにしようとしています。

http://www.server.com/location/

これを指します:

http://www.server.com/subdirectory/index.php?content=location

フォーマットされた URL の末尾にある余分なものはすべて、クエリ文字列の「コンテンツ」パラメーターに追加する必要があるため、次のようになります。

http://www.server.com/location/x/y/z

これを指す必要があります:

http://www.server.com/subdirectory/index.php?content=location/x/y/z

私が行った調査に基づいて、Apache mod-rewrite と Nginx HttpRewriteModule の両方を使用してこれが可能であると確信していますが、動作するようには見えません。これらのセットアップのいずれかまたは両方の式をまとめる方法について誰かが私にいくつかの指針を与えることができれば、私はそれを大いに感謝します. ありがとう!

4

4 に答える 4

5

nginx では、rewrite ディレクティブで「/location」に一致し、末尾の文字列を変数 $1 にキャプチャして、置換文字列に追加します。

server {
...
rewrite ^/location(.*)$ /subdirectory/index.php?content=location$1 break;
...
}

Apache の httpd.conf では、これは非常によく似ています。

RewriteEngine On
RewriteRule ^/location(.*)$ /subdirectory/index.php?content=location$1 [L]

このページの最後にある例をご覧ください: https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html

于 2016-10-30T21:53:38.290 に答える
3

Apache の場合、ドキュメント ルートの htaccess ファイルに次を追加します。

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/subdirectory/index\.php$
RewriteRule ^(.*)$ /subdirectory/index.php?content=$1 [L]

nginx では、最初にリクエスト/subdirectory/index.phpが通過することを確認してから、他のすべてを書き直します。

location ~ /subdirectory/index\.php$ 
{ 
} 

location / 
{ 
    rewrite ^(.*)$ /subdirectory/index.php?content=$1 break; 
}
于 2012-10-18T06:37:25.240 に答える
3

検索文字列:(.+)/location/(.*)$

置換文字列:$1/subdirectory/index.php?content=location/$2

于 2012-10-18T03:11:20.213 に答える
2

これはおそらくnginxでそれを行うための最良の方法でしょう:

location ^~ /location/ {
    rewrite ^/(location/.*)$ /subdirectory/index.php?content=$1 last;
}

詳細については、次を参照してください。

于 2016-11-06T06:35:17.767 に答える