E
E
emellstornn2017-09-20 07:44:19
Drupal
emellstornn, 2017-09-20 07:44:19

Nginx+drupal7+landing pages how to properly set up a redirect?

Hello. There is such a task:
about 400 domains of the same type that differ in endings, for example:
examlpe.com
example.net
examples.com
examples.net
All four requests must be redirected to example.com/?q=example
Current config version:

set $main_host 'example.com';
    if ($host != $main_host) {
        rewrite             ^(.*)$              http://$main_host/?q=example$1     redirect;
        break;
    }

This works, but when you navigate to example.com, of course, nothing happens
if you add a condition like
if ($host = $main_host) {
        rewrite             http://$main_host              http://$main_host/?q=example$1     redirect;
        break;
    }

then my redirects start spinning in a cycle.
Entries of type
rewrite ^ $scheme://example.com\?q=example break;
 return 301 $scheme://example.com\?q=example$request_uri;

in the same way, they redirect in a circle, after which the browser issues an appropriate error.
In addition, for a single example domain, this is not difficult to write, but when there are about 400 of them, a more automated solution would be desirable.
Previously, all this was organized on apache via htaccess with the following rules:
RewriteEngine on

RewriteCond %{HTTP_HOST} ^(.*)parts\.com  [NC]
RewriteRule (.*)$ http://%1part.com [L,R=301]

RewriteCond %{HTTP_HOST} ^(.*)parts\.net  [NC]
RewriteRule (.*)$ http://%1part.com [L,R=301]

RewriteCond %{HTTP_HOST} ^(.*)\.net  [NC]
RewriteRule (.*)$ http://%1.com [L,R=301]
RewriteCond %{HTTP_HOST} ^(.*)\.COM [NC] 
RewriteRule ^$ index.php?q=%1 [NC,QSA]

htaccess->nginx converter (tried 2 pieces) issue either completely non-working rules, or tear off everything up to .com
For example, like this:
if ($http_host ~ "^(.*)parts.com"){
set $rule_0 1$rule_0;
set $bref_1 $1;
}
if ($rule_0 = "1"){
rewrite /(.*)$ http://$bref_1part.com permanent;
}

if ($http_host ~ "^(.*)parts.net"){
set $rule_2 1$rule_2;
set $bref_1 $1;
}
if ($rule_2 = "1"){
rewrite /(.*)$ http://$bref_1part.com permanent;
}
if ($http_host ~ "^(.*).net"){
set $rule_3 1$rule_3;
set $bref_1 $1;
}
if ($rule_3 = "1"){
rewrite /(.*)$ http://$bref_1.com permanent;
}
if ($http_host ~* "^(.*).COM"){
set $rule_4 1$rule_4;
set $bref_1 $1;
}
if ($rule_4 = "1"){
rewrite ^/$ /index.php?q=$bref_1;
}

Please help me figure it out. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Mukovoz, 2017-09-20
@emellstornn

I don't understand why this is so stupid.

server {
    listen       80;
    server_name  .example.*;
    if ($arg !~ q=example) { rewrite ^(.*)$ http://example.com/?q=example$1 redirect; }
}

Read the documentation, it's all very elementary.
https://nginx.ru/ru/docs/http/server_names.html
https://nginx.ru/ru/docs/http/ngx_http_core_module...
https://nginx.ru/ru/docs/http/ngx_http_rewrite_mod ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question