【发布时间】:2015-06-24 01:01:29
【问题描述】:
从 Apache 迁移到 Nginx 时,必须将 .htaccess 文件中的某些规则“翻译”到 Nginx 配置文件。 一个我似乎无法解决的问题,一个例子是最简单的解释方式:
请求http://www.domain.com/nginx被Apache内部重写为index.php?option=com_content&view=article&id=145
现在我想阻止对index.php?option=com_content 的直接请求,因此该页面只能通过http://www.domain.com/nginx 使用,以避免重复内容。在 Apache 中,这是通过使用这些 .htaccess 规则来实现的:
# Check if it's the first pass to prevent a loop. In case of first pass, the environment variable contains nothing
# If http://www.domain.com/nginx already internally has been rewritten to index.php?option=com_content&view=article&id=145 {ENV:REDIRECT_STATUS} contains '200' and the request is allowed to be processed
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# Check if the query string contains requests for the page
RewriteCond %{QUERY_STRING} ^index.php?option=com_content&view=article&id=145 [NC]
# If conditions apply, reject request
RewriteRule .* 404 [L]
在 Nginx 中,有没有我可以使用的环境变量? 或者我应该以完全不同的方式处理这个问题?
编辑 1: 在现实生活中,它不仅仅是一个页面,而是一个包含大量页面的动态 Joomla 站点。我测试了上面的方法,但目的是阻止 index.php?option_content&view=article&id=*
上的所有请求编辑 2: 这是工作的 NGINX 配置文件:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html/domainname;
index index.php index.html index.htm;
server_name domainname.com;
server_name localhost;
location / {
try_files $uri $uri/ /index.php?$args;
}
# deny running scripts inside writable directories
location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
return 403;
error_page 403 /403_error.html;
}
## give 404 header & redirect to custom errorpage without changing URL ##
error_page 404 = /404_custom.php; #global error page, script handles header
error_page 500 502 503 504 /50x.html;
location =/index.php {
set $arg_set "${arg_option}___${arg_view}___${arg_id}";
if ($arg_set ~* "^(((\w|-)+?)___){2}((\w|-)+?)$") {
return 404;
}
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
【问题讨论】:
-
有指令
internalnginx.org/r/internal
标签: .htaccess nginx seo rewrite