I'm new to nginx's rewrite engine. I'm trying to convert my old htaccess file over to nginx's format but I'm running into some trouble.
# ------------------------------------------------------ #
# 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]
That's my htaccess file. Now here's what I've got so far…
# ------------------------------------------------------ #
# 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;
}
The index.php redirect works fine, as does the "directory" name -> php file redirect. However, I can't figure out how to do several things: Implement the trailing-slash fix, and externally redirecting .php files so that I don't have any duplicate files. I want all pages to look clean, like /help, /about, etc. The real pages on the server are /about.php format. Also, I can't get the rewrite rule for the images working. I want anything that's not a real file or directory (-e flag) and not a rewritable file (like /about) to redirect to generation/etc…
Best Answer
You're going about it wrong. Nginx loves location blocks, for instance to redirect /index.php to / you'd do.
You'll also want to look into try_files. In general, if you're using an if in Nginx then chances are you're doing it wrong.
I recommend reading this for a basic introduction on how Nginx uses server blocks and location blocks: http://blog.martinfjordvald.com/2010/07/nginx-primer/