
私は nginx の書き換えエンジンを初めて使用します。古い htaccess ファイルを nginx の形式に変換しようとしているのですが、問題が発生しています。
# ------------------------------------------------------ #
# This redirects index.php to / #
# ------------------------------------------------------ #
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(index|index\.php)\ HTTP/
RewriteRule ^(index|index\.php)$ http://domain.com/ [R=301,L]
# ------------------------------------------------------ #
# This rewrites 'directories' to their PHP files, #
# fixes trailing-slash issues, and redirects .php #
# to 'directory' to avoid duplicate content. #
# ------------------------------------------------------ #
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)/$ http://twitstamp.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^.]+\.php\ HTTP/
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)\.php$ http://twitstamp.com/$1 [R=301,L]
# ------------------------------------------------------ #
# If it wasn't redirected previously and is not #
# a file on the server, rewrite to image #
# ------------------------------------------------------ #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9_\-@#\ "'\+]+)/?([a-z0-9_\-]+)?(\.png|/)?$ generation/image.php?user=${escapemap:$1}&template=${escapemap:$2} [NC,L]
これが私の htaccess ファイルです。これが私がこれまでに作成したものです...
# ------------------------------------------------------ #
# This redirects index.php to / #
# ------------------------------------------------------ #
if ($request_uri ~* "^/index.php\??$") {
rewrite ^.*$ http://$host? permanent;
}
# ------------------------------------------------------ #
# This rewrites 'directories' to their PHP files, #
# fixes trailing-slash issues, and redirects .php #
# to 'directory' to avoid duplicate content. #
# ------------------------------------------------------ #
if (!-e $request_filename) {
rewrite ^(.*)$ $1.php;
last;
}
# ------------------------------------------------------ #
# If it wasn't redirected previously and is not #
# a file on the server, rewrite to image #
# ------------------------------------------------------ #
if (!-e $request_filename) {
rewrite ^([a-z0-9_\-@#\ "'\+]+)/?([a-z0-9_\-]+)?(\.png|/)?$ generation/image.php?user=$1&template=$2;
break;
}
index.php リダイレクトは正常に機能し、「ディレクトリ」名 -> php ファイル リダイレクトも同様です。ただし、いくつかの操作方法がわかりません。末尾のスラッシュ修正を実装し、重複ファイルが存在しないように .php ファイルを外部にリダイレクトします。/help、/about などのすべてのページをきれいに表示したいです。サーバー上の実際のページは /about.php 形式です。また、画像の書き換えルールが機能しません。実際のファイルまたはディレクトリ (-e フラグ) ではなく、書き換え可能なファイル (/about など) でもないものを generation/etc にリダイレクトしたいです...
答え1
やり方が間違っています。Nginx はロケーション ブロックを好みます。たとえば、/index.php を / にリダイレクトします。
location = /index.php {
rewrite ^ http://domain.com/$args permanent;
}
try_files も調べてみてください。一般的に、Nginx で if を使用している場合は、間違っている可能性があります。
Nginx がサーバー ブロックとロケーション ブロックをどのように使用するかについての基本的な概要については、以下を読むことをお勧めします。http://blog.martinfjordvald.com/2010/07/nginx-primer/