DNS Rewrite Rules Apache To Ngnix Translation - apache

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.

Related

.htaccess rule to NGINX

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.

Force www with .htaccess behind varnish

I want to force www domain behind varnish and apache.
After installing varnish RewriteRule is not redirecting domain.com to www.domain.com
I have:
apache 2.4.7
varnish 3.0.5
several virtual hosts for multiple domains
My .htaccess directive was working very well until I installed varnish.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
I already tried to find for the solution but without success.
In my vcl I only configured this:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
All other code is commented.
Update your rules with:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
Not ideal, because it hard codes the domain, but should work :)
The rationale, is that HTTP_HOST contains port number and your redirect fails to handle it.
Alternatively, strip port number from HTTP_HOST variable using this in VCL:
sub vcl_recv {
# Normalize the header, remove the port
set req.http.host = regsub(req.http.host, ":[0-9]+", "");
}

Serve the new version of my website to my IP address via httpd.conf or .htaccess

I'm working on a full update for my wp-based website (full rewrite from scratch).
I wonder if it's possible to serve the old website, but serve the new one for my IP address.
Is there a way to achieve this via httpd.conf or .htaccess?
If (IP_Address = 123.456.789) then
# Serve content from /home/website.com/public_html/
else
# Serve content from /home/new-website.com/public_html/
end if
Thanks!
Using mod_rewrite you can use the %{REMOTE_ADDR} variable to detect what the remote ip is, and change your content root based on that:
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^127.0.0.1$
RewriteCond %{REQUEST_URI} !^/newsite/index\.php$
RewriteRule ^ /newsite/index.php
RewriteCond %{REMOTE_ADDR} !^127.0.0.1$
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^ /index.php

Redirecting base domain to HTTPS, and sub-domains to HTTP

I've been using Apache for a project, and have now decided to convert over to nginx for performance reasons as the project has grown quite a bit.
For this project, we serve our base domain and www sub-domain over HTTPS, but need to serve all other sub-domains over HTTP.
In Apache, I was able to accomplish this with the RewriteEngine doing the following:
RewriteEngine On
#Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [OR]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com [NC]
RewriteCond %{HTTP_HOST} !=www.mydomain.com
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have my website about half setup with nginx, and this portion of the configuration has stumped me. How do I go about converting this to work with nginx?
I divided the answer in 4 virtual host. The first two solve the http to https redirect among the main domains. And the second part catches the subdomains and redirects from https to http:
# FIRST PART ---------------
# from http to https on main domains
server {
listen 80;
server_name domain www.domain;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name domain www.domain;
# blah, blah, https and virtualhost configuration
}
# SECOND PART ---------------
# from https to http and others subdomains
server {
listen 443 ssl;
server_name *.domain;
# blah, blah, https configuration
location / {
return 301 http://$host$request_uri
}
}
server {
listen 80;
server *.domain;
# virtual with http configuration
}

Rewrite to a new domain? with url query? not working. :( help

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