C
C
celsoft2018-10-23 13:33:51
htaccess
celsoft, 2018-10-23 13:33:51

apache rules for nginx?

Hello. Please help me migrate htaccess rules to nginx.

nginx config
server {
listen ip:80;
server_name 111.com www.111.com;
root /home/admin/web/111.com/public_html;
index index.php index.html index.htm;
access_log /var/log/nginx/domains/111.com.log combined;
access_log /var/log/nginx/domains/111.com.bytes bytes;
error_log /var/log/nginx/domains/111.com.error.log error;
location / {
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}
location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
error_page 403 /error/404.html;
error_page 404 /error/404.html;
error_page 500 502 503 504 /error/50x.html;
location /error/ {
alias /home/admin/web/111.com/document_errors/;
}
location ~* "/\.(htaccess|htpasswd)$" {
deny all;
return 404;
}
location /vstats/ {
alias /home/admin/web/111.com/stats/;
include /home/admin/conf/web/111.com.auth*;
}
include /etc/nginx/conf.d/phpmyadmin.inc*;
include /etc/nginx/conf.d/phppgadmin.inc*;
include /etc/nginx/conf.d/webmail.inc*;
include /home/admin/conf/web/nginx.111.com.conf*;
}
htaccess
AddDefaultCharset utf-8
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine On
# Бан "плохих" ботов для снижения нагрузки на серверп
RewriteCond %{HTTP_USER_AGENT} (AhrefsBot|MJ12bot|DotBot|BUbiNG\ Crawler|BUbiNG|FaceBook\ Crawler|FaceBook|SemrushBot|LinkdexBot|Alexa\ Robot|Alexa|Ads.txt-crawler|Powermarks|GrapeshotCrawler|grapeshot|SimplePie|360Spider|CCBot|SeznamBot|LinkpadBot|BLEXBot) [NC]
RewriteRule .* - [R=403,L]
# Обратная совместимость со старыми страницами
RewriteRule ^pages/(.*)$ /page/$1 [L,R=301]
# Редирект с WWW
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
# API админ панели
RewriteRule ^admin/api/(.*) /adminko/?act=api&api=$1 [NS,L]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2018-10-23
@celsoft

# Бан "плохих" ботов для снижения нагрузки на серверп
RewriteCond %{HTTP_USER_AGENT} (AhrefsBot|MJ12bot|DotBot|BUbiNG\ Crawler|BUbiNG|FaceBook\ Crawler|FaceBook|SemrushBot|LinkdexBot|Alexa\ Robot|Alexa|Ads.txt-crawler|Powermarks|GrapeshotCrawler|grapeshot|SimplePie|360Spider|CCBot|SeznamBot|LinkpadBot|BLEXBot) [NC]
RewriteRule .* - [R=403,L]

# Обратная совместимость со старыми страницами
RewriteRule ^pages/(.*)$ /page/$1 [L,R=301]

# Редирект с WWW
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

These three rules on nginx will look like this:
server {
listen ip:80;
server_name www.111.com;
return 301 http://111.com$request_uri;
}

server {
listen ip:80;
server_name 111.com;

if ($http_user_agent ~* "(AhrefsBot|MJ12bot|DotBot|BUbiNG\ Crawler|BUbiNG|FaceBook\ Crawler|FaceBook|SemrushBot|LinkdexBot|Alexa\ Robot|Alexa|Ads.txt-crawler|Powermarks|GrapeshotCrawler|grapeshot|SimplePie|360Spider|CCBot|SeznamBot|LinkpadBot|BLEXBot)") {
    return 403;
}

rewrite ^/pages/(.*)$ /page/$1 permanent;

This snippet should be put in place of yours
server {
listen ip:80;
server_name 111.com www.111.com;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question