I have the following .httaccess rule which changes path to logo depending on host. If host ".ru" it takes from /img/up/ru/b-logo instead of /img/up/b-logo
RewriteCond %{HTTP_HOST} example\.ru [NC]
RewriteCond %{REQUEST_URI} ^(/img/up/)(b-logo)(.*)$ [NC]
RewriteRule (.*) %1ru/%2%3 [L]
Can anybody help me rewrite it for nginx?
Rules from different converters do not work
Try something like this:
map $http_host$uri $lng {
default $uri;
~^example\.ru/img/up/b-logo(.*) /img/up/ru/b-logo$1;
}
server
server_name example.com example.ru;
...
location /img/up/b-logo {
rewrite .* $lng$is_args$args;
}
...
}
nginx location selection algorithm is quite complex (description in russian language), make sure that this location block will take priority against other defined locations.
Related
In my code I am trying to convert Apache .htaccess into Nginx using online convertor. I even edited this file etc/nginx/conf.d/domainname.conf but the rewrite URLS won't work. I have two htaccess files, one of which used in root folder and the second one used in administration folder of my script.
Here is the root's htaccess file content
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?slug=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ index.php?slug=$1
htaccess converted code for Nginx
location / {
rewrite ^/([a-zA-Z0-9-/]+)$ /index.php?slug=$1;
rewrite ^/([a-zA-Z0-9-/]+)/$ /index.php?slug=$1;
}
Here is the administration folder htaccess file content
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* $0.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^(.*)/$ /$1.php [L,R=301]
Administration folder htaccess converted code for Nginx
location /administration {
if (!-e $request_filename){
rewrite ^(.*)$ /$0.php break;
}
rewrite ^/(.*)/$ /$1.php redirect;
}
I pasted the converted content into domainname.conf (domainname is my domain) and restart the ngnix but it won't work at all. I don't know whether my converted code is accurate or not or any thing else I am missing through it.
Simply edit the file i.e. etc/nginx/conf.d/yourdomain.tld.conf and look for the following code like this:
#location / {
#Uncomment 3 lines and set your rules here!
#}
You need to un-comment these 3 lines of code or paste this code instead:
location / {
if (!-e $request_filename) {
rewrite ^/([a-zA-Z0-9-/]+)$ /index.php?slug=$1;
rewrite ^/([a-zA-Z0-9-/]+)/$ /index.php?slug=$1;
}
}
location /administration/ {
if (!-e $request_filename){
rewrite ^(.*)$ $1.php last;
}
}
Then you must require to restart Nginx, if restarted without any errors it will work accurately otherwise Nginx will stop working, this step is important.
I need you help i use to successfully migrate my site from Apache to Ngnix but i trying to convert some DNS rewrite rules and till now with no success . Can you please give me some translation help .
Rewrites that i wont to translate are :
RewriteCond %{HTTP_HOST} ^domain\.net\.mk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.net\.mk$
RewriteRule ^(.*)$ "http\:\/\/domain\.com\.mk\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.mk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.mk$
RewriteRule ^(.*)$ "http\:\/\/domain\.com\.mk\/$1[R=301,L]
Thanks for you support , where do a need to put translated rewrites in my vhost template or in nginx.conf (single web site on VPS)
To redirect a number of domains to another domain use a separate server block, for example:
server {
listen 80;
server_name domain.net.mk www.domain.net.mk domain.mk www.domain.mk;
return 301 http://domain.com.mk$request_uri;
}
server {
listen 80;
server_name domain.com.mk;
...
}
See this document for more.
I have these apache (htaccess) rules and I want to convert them to nginx rules. I have tried a lot of combinations but in vain.
You have the rules below:
RewriteRule ^candybooru/_images/([0-9a-f]{2})([0-9a-f]{30}).*$ /candybooru/images/$1/$1$2
RewriteRule ^candybooru/_thumbs/([0-9a-f]{2})([0-9a-f]{30}).*$ /candybooru/thumbs/$1/$1$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^candybooru/(.*)$ /candybooru/index.php?q=$1&%{QUERY_STRING}
Maybe you could use this page to convert the rules: http://winginx.com/en/htaccess
The converted rule is:
# nginx configuration
location / {
rewrite "^/candybooru/_images/([0-9a-f]{2})([0-9a-f]{30}).*$" /candybooru/images/$1/$1$2;
rewrite "^/candybooru/_thumbs/([0-9a-f]{2})([0-9a-f]{30}).*$" /candybooru/thumbs/$1/$1$2;
}
location /candybooru {
rewrite ^/candybooru/(.*)$ /candybooru/index.php?q=$1&$query_string;
}
Check out try_files. I haven't tested this, but it should work (or come close):
location /candybooru/ {
location ~ "^/candybooru/_(images|thumbs)/([0-9a-f]{2})([0-9a-f]{30})" {
try_files /candybooru/$1/$2/$2$3 /candybooru/index.php?q=$2&$query_string;
}
}
i'm trying to rewrite the htaccess rule to nginx rule.
this rule is used to load a php page for robot (like google robot).
here my htaccess:
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteCond %{REQUEST_URI} ^/faq/$
RewriteCond %{QUERY_STRING} ^(.*)_escaped_fragment_=(.*)$
RewriteRule ^(.*)$ /files/snapshot_loader.php?snapshot_page=%1%2 [L]
can you help me ? i try this one, but redirect never work....
location /faq {
if ($query_string ~ "^(.*)test(.*)$"){
RewriteRule ^(.*)$ http://www.google.com permanent
}
}
Thanks in advance
First, there is no $query_string variable in nginx. And the second thing is there is no RewriteRule directive in nginx.
In nginx, the query string is stored in $args and you want the rewrite directive:
Try something like:
location /faq {
if ($http_host ~* "^localhost$") {
if($args ~* "^(.*)_escaped_fragment_=(.*)$") {
set $newargs $1$2
set $args '';
rewrite ^ /files/snapshot_loader.php?snapshot_page=$newargs
}
}
}
I have this rewrite rule...
Redirect all initial request from world.example.com to web.example.com
RewriteCond %{HTTP_HOST} ^world\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^(.*)$ https://web.example.com$1 [R=301,L]
Which works great. But, some of my applications has...
https://world.example.com/approvals/?id=b47b5256567
Unfortunately, it is not being redirected properly to web.example.com. Instead, it just goes to web.example.com without the query params.
How could I make all request redirect properly to web.example.com along with the query params?
Basically, what it should do...
https://world.example.com/approvals/?id=b47b5256567
then
https://web.example.com/approvals/?id=b47b5256567
Just changing the world to web and passing the query string.
Help
You don't need to use the complicated rewrite engine to do this. Just use Redirect:
<VirtualHost *>
ServerName world.example.com
Redirect permanent / https://web.example.com/
</VirtualHost>
The Redirect directive automatically preserves everything that comes after what you're redirecting.
You forgot to use the "Query String Append" flag [R=301,L,QSA].
We can do it with Nginx, too.
server {
listen 80:
server_name: world.example.com
# URL redirect
#rewrite ^ http://web.example.com$request_uri? permanent;
rewrite ^ $scheme://web.example.com$request_uri permanent;
#rewrite ^ $scheme://web.example.com permanent;
#rewrite ^ $scheme://web.example.com;
}
Reference:
Nginx Redirect (Rewrite) Old Domain To New Domain With HTTP 301