coverting .htacess to nginx config - apache

This the htacess code
RewriteEngine on
ErrorDocument 404 /404.php
RewriteRule ^watch/(.*)/([0-9]+)/(.*)/?$ search.php?search=$1&page=$2&token=$3
RewriteRule ^video/(.*)/(.*)/?$ watch.php?link=$1&name=$2
RewriteRule ^privacy/?$ privacy.php
RewriteRule ^dmca/?$ dmca.php
RewriteRule ^contact/?$ contact.php
RewriteRule ^send/?$ send.php
RewriteRule ^surprise/?$ surprise.php
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
what should be the correct nginx config?

nginx configuration
error_page 404 /404.php;
location /watch {
rewrite ^/watch/(.*)/([0-9]+)/(.*)/?$ /search.php?search=$1&page=$2&token=$3;
}
location /video {
rewrite ^/video/(.*)/(.*)/?$ /watch.php?link=$1&name=$2;
}
location /privacy {
rewrite ^/privacy/?$ /privacy.php;
}
location /dmca {
rewrite ^/dmca/?$ /dmca.php;
}
location /contact {
rewrite ^/contact/?$ /contact.php;
}
location /send {
rewrite ^/send/?$ /send.php;
}
location /surprise {
rewrite ^/surprise/?$ /surprise.php;
}
location / {
}

Related

htaccess rewrite to nginx config not working

I'm having a problem converting an appache .htaccess file work with nginx.
The .htaccess file
<ifModule mod_rewrite.c>
Allow from 127.0.0.1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index(|/)$ index.php
RewriteRule ^logout(|/)$ logout.php
RewriteRule ^keeping/([^/.]+)(|/)$ keeping.php?s=$1
</ifModule>
My Conversion:
location / {
if (!-e $request_filename){
rewrite ^/index(|/)$ /index.php;
}
}
location /logout {
rewrite ^/logout(|/)$ /logout.php;
}
location /kyhsadminpanel {
rewrite ^/keeping/([^/.]+)(|/)$ /keeping.php?s=$1;
}
doesn't work.
It always shows "404 not found" for url's such as /kyhsadminpanel/keeping/index
and /kyhsadminpanel/keeping/news...
You might want to note you used: https://winginx.com/en/htaccess
I tend to use: https://timmehosting.de/htaccess-converter
Here is the result from them.
if (!-d $request_filename){
set $rule_0 1$rule_0;
}
if (!-f $request_filename){
set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
rewrite ^/index(|/)$ /index.php ;
}
rewrite ^/logout(|/)$ /logout.php ;
rewrite ^/keeping/([^/.]+)(|/)$ /keeping.php?s=$1 ;

Can G6 Firewall be converted to be used on NGINX?

I'm a big fan of the G6 firewall (previously G5) htaccess rules. I was wondering if it would be possible, as well as beneficial, to convert this to be used with NGINX? The source article can be found here: https://perishablepress.com/6g/. If it's possible to rewrite this, would you provide the NGINX equivalent rule set? I think it would be helpful to a lot of developers out there.
Here's the orignal .htaccess rules:
# 6G FIREWALL/BLACKLIST
# # https://perishablepress.com/6g/
# 6G:[QUERY STRINGS]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} (eval\() [NC,OR]
RewriteCond %{QUERY_STRING} (127\.0\.0\.1) [NC,OR]
RewriteCond %{QUERY_STRING} ([a-z0-9]{2000}) [NC,OR]
RewriteCond %{QUERY_STRING} (javascript:)(.*)(;) [NC,OR]
RewriteCond %{QUERY_STRING} (base64_encode)(.*)(\() [NC,OR]
RewriteCond %{QUERY_STRING} (GLOBALS|REQUEST)(=|\[|%) [NC,OR]
RewriteCond %{QUERY_STRING} (<|%3C)(.*)script(.*)(>|%3) [NC,OR]
RewriteCond %{QUERY_STRING} (\\|\.\.\.|\.\./|~|`|<|>|\|) [NC,OR]
RewriteCond %{QUERY_STRING} (boot\.ini|etc/passwd|self/environ) [NC,OR]
RewriteCond %{QUERY_STRING} (thumbs?(_editor|open)?|tim(thumb)?)\.php [NC,OR]
RewriteCond %{QUERY_STRING} (\'|\")(.*)(drop|insert|md5|select|union) [NC]
RewriteRule .* - [F]
</IfModule>
# 6G:[REQUEST METHOD]
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_METHOD} ^(connect|debug|delete|move|put|trace|track) [NC]
RewriteRule .* - [F]
</IfModule>
# 6G:[REFERRERS]
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_REFERER} ([a-z0-9]{2000}) [NC,OR]
RewriteCond %{HTTP_REFERER} (semalt.com|todaperfeita) [NC]
RewriteRule .* - [F]
</IfModule>
# 6G:[REQUEST STRINGS]
<IfModule mod_alias.c>
RedirectMatch 403 (?i)([a-z0-9]{2000})
RedirectMatch 403 (?i)(https?|ftp|php):/
RedirectMatch 403 (?i)(base64_encode)(.*)(\()
RedirectMatch 403 (?i)(=\\\'|=\\%27|/\\\'/?)\.
RedirectMatch 403 (?i)/(\$(\&)?|\*|\"|\.|,|&|&?)/?$
RedirectMatch 403 (?i)(\{0\}|\(/\(|\.\.\.|\+\+\+|\\\"\\\")
RedirectMatch 403 (?i)(~|`|<|>|:|;|,|%|\\|\s|\{|\}|\[|\]|\|)
RedirectMatch 403 (?i)/(=|\$&|_mm|cgi-|etc/passwd|muieblack)
RedirectMatch 403 (?i)(&pws=0|_vti_|\(null\)|\{\$itemURL\}|echo(.*)kae|etc/passwd|eval\(|self/environ)
RedirectMatch 403 (?i)\.(aspx?|bash|bak?|cfg|cgi|dll|exe|git|hg|ini|jsp|log|mdb|out|sql|svn|swp|tar|rar|rdf)$
RedirectMatch 403 (?i)/(^$|(wp-)?config|mobiquo|phpinfo|shell|sqlpatch|thumb|thumb_editor|thumbopen|timthumb|webshell)\.php
</IfModule>
# 6G:[USER AGENTS]
<IfModule mod_setenvif.c>
SetEnvIfNoCase User-Agent ([a-z0-9]{2000}) bad_bot
SetEnvIfNoCase User-Agent (archive.org|binlar|casper|checkpriv|choppy|clshttp|cmsworld|diavol|dotbot|extract|feedfinder|flicky|g00g1e|harvest|heritrix|httrack|kmccrew|loader|miner|nikto|nutch|planetwork|postrank|purebot|pycurl|python|seekerspider|siclab|skygrid|sqlmap|sucker|turnit|vikspider|winhttp|xxxyy|youda|zmeu|zune) bad_bot
<limit GET POST PUT>
Order Allow,Deny
Allow from All
Deny from env=bad_bot
</limit>
</IfModule>
# 6G:[BAD IPS]
<Limit GET HEAD OPTIONS POST PUT>
Order Allow,Deny
Allow from All
# uncomment/edit/repeat next line to block IPs
# Deny from 123.456.789
</Limit>
Thank you in advance for your help or thoughts on this and a big shout-out to Perishable Press for writing this htaccess. Thank you!
## Add here all user agents that are to be blocked.
map $http_user_agent $bad_bot {
default 0;
"~*([a-z0-9]{2000})" 1;
~*(archive.org|binlar|casper|checkpriv|choppy|clshttp|cmsworld|diavol|dotbot|extract|feedfinder|flicky|g00g1e|harvest|heritrix|htmlparser|libwww|httrack|kmccrew|loader|miner|nikto|nutch|planetwork|postrank|purebot|pycurl|python|seekerspider|siclab|skygrid|sqlmap|sucker|turnit|vikspider|winhttp|xxxyy|youda|zmeu|zune) 1;
}
## Add here all referrers that are to blocked.
map $http_referer $bad_referer {
default 0;
"~*([a-z0-9]{2000})" 1;
~*(semalt.com|todaperfeita) 1;
}
# query strings that should be blocked
map $query_string $bad_querystring {
default 0;
~*(eval\() 1;
~*(127\.0\.0\.1) 1;
"~*([a-z0-9]{2000})" 1;
"~*(javascript:)(.*)(;)" 1;
~*(base64_encode)(.*)(\() 1;
~*(GLOBALS|REQUEST)(=|\[|%) 1;
~*(<|%3C)(.*)script(.*)(>|%3) 1;
~*(\\|\.\.\.|\.\./|~|`|<|>|\|) 1;
~*(boot\.ini|etc/passwd|self/environ) 1;
~*(thumbs?(_editor|open)?|tim(thumb)?)\.php 1;
~*(\'|\")(.*)(drop|insert|md5|select|union|concat) 1;
}
map $request_uri $bad_request {
default 0;
"~*([a-z0-9]{2000})" 1;
~*(https?|ftp|php):/ 1;
~*(base64_encode)(.*)(\() 1;
~*(=\\\'|=\\%27|/\\\'/?)\. 1;
"~*/(\$(\&)?|\*|\"|\.|,|&|&?)/?$" 1;
~*(\{0\}|\(/\(|\.\.\.|\+\+\+|\\\"\\\") 1;
"~*(~|`|<|>|:|;|,|%|\\|\s|\{|\}|\[|\]|\|)" 1;
~*/(=|\$&|_mm|cgi-|etc/passwd|muieblack) 1;
"~*(&pws=0|_vti_|\(null\)|\{\$itemURL\}|echo(.*)kae|etc/passwd|eval\(|self/environ)" 1;
~*\.(aspx?|bash|bak?|cfg|cgi|dll|exe|git|hg|ini|jsp|log|mdb|out|sql|svn|swp|tar|rar|rdf)$ 1;
~*/(^$|(wp-)?config|mobiquo|phpinfo|shell|sqlpatch|thumb|thumb_editor|thumbopen|timthumb|webshell)\.php 1;
}
map $request_method $not_allowed_method {
default 0;
~*^(connect|debug|delete|move|put|trace|track) 1;
}
Then in the server directive I add this
server {
...
if ($bad_bot) { return 444; }
if ($bad_referer) { return 444; }
if ($bad_querystring) { return 444; }
if ($bad_request) { return 444; }
if ($not_allowed_method) { return 405; }
## Filesystem root of the site and index.
root /home/$DOMAIN/public;
index index.php;
...
}

How to convert .htaccess to a nginx equivalent?

How to convert .htaccess configuration to nginx?
My .htacess:
Options -MultiViews
RewriteEngine On
RewriteBase /mvc/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I tried this:
nginx configuration
location /mvc/public/ {
if (!-e $request_filename){
rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url=$1 [QSA,L];
}
}
But this did not work!
Could someone help me?
The [QSA,L] is not nginx syntax - see this document for details.
location /mvc/public/ {
if (!-e $request_filename) {
rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url=$1 last;
}
}
Similar behaviour can be accomplished with try_files rather than the if:
location /mvc/public/ {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/mvc/public/(.+)$ /mvc/public/index.php?url=$1 last;
}
See this document for details.

Convert Apache rewrite rules to Nginx

How do I convert these rules from .htaccess (apache) to Nginx conf?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1-$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
I use these rules to rewrite requests like:
/someurl1/ => someurl.html (/company/ to /company.html
/someurl1/someurl2/ => someurl-someurl2.html (/projects/3/ to /projects-3.html)
I've already tried this (doesn't work):
if (!-e $request_filename) {
rewrite ^/([^/]+)/$ /$1.html;
rewrite ^/([^/]+)/([^/]+)/$ /$1-$2.html;
}
try_files $uri $uri/ =404;
Where am I wrong?
I have tested the following and it appears to work:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/([^/]+)/$ /$1.html last;
rewrite ^/([^/]+)/([^/]+)/$ /$1-$2.html last;
return 404;
}
The RewriteCond %{REQUEST_FILENAME} !-f test is actually accomplished by the try_files directive.
See this for details.

htaccess to nginx (help)

I am trying to convert the code from the htaccess to Nginx but without any success, and yes i have tried all the online converter but it did not help, so can any here on stockoverflow help me please? I'm going crazy soon :-p (Here is the code from the htaccess)
RewriteCond %{HTTP_HOST} ^www.WEBSITE.com
RewriteRule (.*) http://WEBSITE.com/$1 [R=301,L]
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^([a-z]+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
Please try the following:
server {
listen 80;
server_name www.website.com;
return 301 $scheme://website.com;
}
server {
listen 80;
server_name website.com;
root /path/to/root;
location / {
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^/([a-z]+)/?([0-9a-zA-Z]*)/?.*$ /index.php?a=$1&q=$2 last;
}
}