Help converting Apache HTACCESS directives to nginx?

christek nginx-forum at nginx.us
Mon Aug 9 07:20:11 MSD 2010


Hi, I'm brand new to nginx and I am trying to convert the app I wrote
over from Apache as I need the ability to serve a lot of clients at once
without a lot of overhead! I'm getting the hang of setting up nginx and
FastCGI PHP but I can't wrap my head around nginx's rewrite format just
yet. I know you have to write some simple script that goes in the server
{} block in the nginx config but I'm not yet familiar with the syntax.
Could anyone with experience with both Apache and nginx help me convert
this to nginx format? Thanks!
[code]
Options +FollowSymLinks
RewriteEngine On

# ------------------------------------------------------ #
# Rewrite from canonical domain (remove www.)            #
# and from beta subdomain (remove beta.)                 #
# ------------------------------------------------------ #

	RewriteCond %{HTTP_HOST} ^www.domain.com
	RewriteRule (.*) http://domain.com/$1 [R=301,L]
	
	RewriteCond %{HTTP_HOST} ^beta.domain.com
	RewriteRule (.*) http://domain.com/$1 [R=301,L]

# ------------------------------------------------------ #
# This is for maintenance mode (if /msg.html exists)     #
# ------------------------------------------------------ #

	# Rewrite everything else and attempt a TwitStamp
	# Not a file, directory, or file.php
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteCond %{DOCUMENT_ROOT}/$1.php !-f
	RewriteCond %{DOCUMENT_ROOT}/msg.html -f
	RewriteRule ^([a-z0-9_\-@#\ "'\+]+)/?([a-z0-9_\-]+)?(\.png|/)?$
generation/image.php?user=${escapemap:$1}&template=${escapemap:$2}
[NC,L]

	# Redirect everything to maintenance page
	# Not msg.html, not image.php, not from my IP
	RewriteCond %{REQUEST_URI} !/msg.html$
	RewriteCond %{REQUEST_URI} !/generation/image.php$
	RewriteCond %{REMOTE_HOST} !^76\.178\.84\.13
	RewriteCond %{REMOTE_HOST} !^76\.19\.200\.16
	RewriteCond %{DOCUMENT_ROOT}/msg.html -f
	RewriteRule $ /msg.html [NC,L]
	
# ------------------------------------------------------ #
# 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://domain.com/$1 [R=301,L]

	RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^.]+\.php\ HTTP/
	RewriteCond %{DOCUMENT_ROOT}/$1.php -f
	RewriteRule ^([^.]+)\.php$ http://domain.com/$1 [R=301,L]

# ------------------------------------------------------ #
# If it wasn't redirected previously and is not          #
# a file on the server, rewrite to a TwitStamp           #
# ------------------------------------------------------ #

	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]
[/code]

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,117689,117689#msg-117689




More information about the nginx mailing list