I try to port my previous .htaccess (used with apache) to nginx:
<IfModule rewrite_module>
RewriteEngine on
RewriteCond "%{HTTP_USER_AGENT}" "(Googlebot|bingbot|slackbot|vkShare|W3C_Validator)" [NC]
RewriteRule .* bot.php [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.html [L]
</IfModule>
Following is, what I currently try:
I have generate a map as List of SearchEngine:
map $http_user_agent $search_engines {
default 0;
"~bingbot.*" 1;
"~BingPreview.*" 1;
"~Googlebot.*" 1;
}
if ($search_engines = 1){
rewrite ^/(.*) bot.php?$1 break;
}
But this creates an invinite Loop.
Here is the full server-block:
server {
server_name mypage.de www.mypage.de;
listen 1.1.1.1;
root /home/mypage/public_html;
index index.html index.htm index.php;
access_log /var/log/virtualmin/mypage.de_access_log;
error_log /var/log/virtualmin/mypage.de_error_log;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME /home/mypage/public_html$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT /home/mypage/public_html;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTPS $https;
location ~ \.php(/|$) {
try_files $uri =404;
fastcgi_pass unix:/var/php-nginx/123123123123123123.sock/socket;
}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
listen 1.1.1.1:443 ssl;
ssl_certificate /home/mypage/ssl.combined;
ssl_certificate_key /home/mypage/ssl.key;
if ($blocked_bots = 1) {
return 444; # Connection closed without response
}
if ($search_engines = 1){
rewrite ^/(.*) /bot.php?$1 break;
}
if ($scheme = http) {
rewrite ^/(?!.well-known)(.*) https://mypage/$1 break;
}
location / {
try_files $uri /index.html;
auth_basic "Administrator’s Area";
auth_basic_user_file /home/mypage/.htpasswd;
}
# Cache-Controll
include /etc/nginx/conf.d/manuallyInclude/cache-policy.conf;
}
2nd Question: I have another mapped variarble for social_network bots. Do I really need to add for each mapping an own if-clause like this:
if ($search_engines = 1){
rewrite ^/(.*) /bot.php?$1 break;
}
if ($social_networks = 1){
rewrite ^/(.*) /bot.php?$1 break;
}
or is there an easier way to combine those to one rewrite-rule?
Answering your second question, use an empty string instead of "0" on your
map
translations (and since the defaultmap
value is exactly an empty string, you can omit thedefault
line at all):and use concatenation of variables for the final condition decision: