如何将我的 .htaccess 文件转换为 NGINX?

Posted

技术标签:

【中文标题】如何将我的 .htaccess 文件转换为 NGINX?【英文标题】:How would I convert my .htaccess file to NGINX? 【发布时间】:2011-01-01 12:24:25 【问题描述】:

我将如何转换下面的 .htaccess (Apache) 文件以便 nginx 可以使用它?

Options -Indexes
Options +FollowSymLinks

# Enable ETag
#FileETag MTime Size
FileETag none

# Set expiration header
ExpiresActive on
ExpiresDefault A2592000
Header append Cache-Control "public"

# Compress some text file types
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/x-javascript text/javascript application/javascript application/json

# Deactivate compression for buggy browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# Set header information for proxies
Header append Vary User-Agent

########################################################
# Rewrite Rules
########################################################

RewriteEngine on

# Require SSL (HTTPS) on the signup page
RewriteCond %SERVER_PORT 80
RewriteCond %REQUEST_URI ^/signup/?
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

# Redirect /signup/plan or /signup/plan/ ->  /signup/index.php?account_type=plan
RewriteRule ^signup/([A-Za-z]+)/?$ /signup/index.php?account_type=$1 [NC,L]

# Redirect /home/123 or /home/123/ ->  home.php?home_id=123
RewriteRule ^home/([0-9]+)/?$ home.php?home_id=$1 [NC,L]

# Redirect /homes/ in case someone made a typo when it should have been /home/
RewriteRule ^homes/([0-9]+)/?$ home.php?home_id=$1 [NC,L]

#################################################
# Default Settings
#################################################

# hide apache server signaute on apache generated pages (e.g. 404)
ServerSignature Off

【问题讨论】:

【参考方案1】:

你可以试试http://www.anilcetin.com/convert-apache-htaccess-to-nginx/

【讨论】:

【参考方案2】:
    if ($server_port ~ "80")
 set $rule_0 1$rule_0;

if ($uri ~ "^/signup/?")
 set $rule_0 2$rule_0;

if ($rule_0 = "21")
 rewrite ^/(.*)$ https://www.example.com/$1 redirect;
 break;

 rewrite ^/signup/([A-Za-z]+)/?$ /signup/index.php?account_type=$1 last;
 rewrite ^/home/([0-9]+)/?$ /home.php?home_id=$1 last;
 rewrite ^/homes/([0-9]+)/?$ /home.php?home_id=$1 last;

Online converter

【讨论】:

以上是关于如何将我的 .htaccess 文件转换为 NGINX?的主要内容,如果未能解决你的问题,请参考以下文章

如何将我的 Java 程序转换为 .exe 文件?

如何在 Nginx 中使用 .htaccess 文件?

如何使用 vim 将我的文件转换为 utf8?

如何将我的 .tar.gz 文件转换为 shutil.copyfileobj 的类文件对象?

如何使用htaccess文件创建干净的URL

如何将我的应用转换为 Android TV 应用?