【问题标题】:Help with Apache .htaccess file帮助 Apache .htaccess 文件
【发布时间】:2011-05-17 13:59:42
【问题描述】:

尝试将我的 .htaccess 文件转换为 NGINX 表示法。

我已阅读在线帮助指南,但仍在苦苦挣扎。

有人愿意帮忙吗?我的 .htaccess 是:

Options -Indexes
Options +FollowSymLinks  

# Enable ETag
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] 

###########################################################
# Error Handling
###########################################################

#ErrorDocument 400 /
#ErrorDocument 401 /
#ErrorDocument 403 /
#ErrorDocument 404 /
#ErrorDocument 500 /


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

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

更新:

对于 GZIP 压缩,这似乎可行。但我还没有弄清楚我的 HTTP 重写规则的方法。

 gzip             on;
  gzip_min_length  1000;
  gzip_proxied     expired no-cache no-store private auth;
  gzip_types       text/plain application/xml;
  gzip_disable     "MSIE [1-6]\.";

更新 2

我尝试用以下 NGINX 规则替换我的 htaccess 重写规则,但这似乎不起作用。任何想法我做错了什么?

# Redirect /signup/planname or /signup/planname/ ->  /signup/index.php?account_type=planname
rewrite  ^signup/([A-Za-z]+)/?$ /signup/index.php?account_type=$1  last;

# Redirect /home/123 or /home/123/ ->  home.php?home_id=123
rewrite  ^home/([0-9]+)/?$ home.php?home_id=$1  last;

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

更新 3

根据下面的 cmets,我现在有以下 NGINX 配置,但仍然有问题

# gzip
gzip             on;
gzip_min_length  1000;
gzip_proxied     expired no-cache no-store private auth;
gzip_types       text/plain application/xml;
gzip_disable     "MSIE [1-6]\.";


# Require SSL (HTTPS) on the signup page
# ====== THIS DOESN'T WORK AND BREAKS NGINX
# I obviously change "example.com" to be my actual domain
if (location /signup/) {
   rewrite ^ https://www.example.com$request_uri? permanent;
}


# Redirect /signup/planname or /signup/planname/ ->  /signup/index.php?account_type=planname
rewrite  ^signup/([A-Za-z] +)/?$ /signup/index.php?account_type=$1  last;

# Redirect /home/123 or /home/123/ ->  home.php?home_id=123
# Also, Redirect /homes/ in case someone made a typo when it should have been /home/
rewrite  ^/homes?/([0-9]+)/?$  /home.php?home_id=$1?  last;

【问题讨论】:

    标签: apache .htaccess configuration nginx


    【解决方案1】:

    正如我在documentation 中看到的,你的规则(不是条件)应该是这样:

    答:

     RewriteCond %{SERVER_PORT} 80 
     RewriteCond %{REQUEST_URI} ^/signup/?
     RewriteRule ^(.*)$ https://www.example.com/$1
    

    N(请参阅here 了解更多信息):

    if (location /signup) {
       rewrite ^ https://pma.clinicacgm.0$request_uri? permanent;
    }
    

    答:

    RewriteRule ^home/([0-9]+)/?$ home.php?home_id=$1
    RewriteRule ^homes/([0-9]+)/?$ home.php?home_id=$1
    

    N:

     rewrite  ^/homes?/([0-9]+)/?$  /home.php?home_id=$1?  last;
    

    【讨论】:

    • 谢谢艾尔。不过问题是,我只希望在最后一条规则中允许 # (数字)。看起来您制定的规则允许在使用“(。*)”时使用任何类型的字符,我如何只将其设为 #?
    • 对不起,我刚从wiki上复制过来。。应该有([0-9]+)([0-9]+)?(我没有服务器很难测试它们)
    • 因此,知道为什么我的 UPDATE 2 不起作用,因为这正是我所做的
    • 但我不知道是否会有尾随“/”。我的 Apache 配置所做的是无论是否存在尾随“/”,都重写为 home.php?home_id=$1。那我该怎么做呢?
    • 来自文档:如果在替换参数行中指示,则其余请求参数将附加到它们。为避免附加它们,请将问号作为最后一个字符
    猜你喜欢
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 2010-10-29
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多