0

I'm very new to nginx, but have previous experience with apache redirects. I converting a subdomain to a subfolder and need some help with getting my rewrite rules correct for my switch. The subdomain is hosted on an apache server, with the new subdirectory hosted on my new nginx server.

Below is the working htaccess file containing my desired redirects:

Options +FollowSymlinks 
RewriteEngine On

RewriteCond %{HTTP_HOST}  ^france-property\.frenchentree\.com$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^for-sale/(.*)$ http://www.frenchentree.com/property-for-sale/search/$1? [R=301,NE,NC,L]

RewriteCond %{HTTP_HOST}  ^france-property\.frenchentree\.com$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^for-sale$ http://www.frenchentree.com/property-for-sale/search? [R=301,NE,NC,L]

RewriteCond %{HTTP_HOST}  ^france-property\.frenchentree\.com$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^for-sale-details(.*) http://www.frenchentree.com/property-for-sale/details$1? [R=301,NE,NC,L]

RewriteCond %{HTTP_HOST}  ^france-property\.frenchentree\.com$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^buyer-registration(.*)$ http://www.frenchentree.com/property-for-sale/buyer-registration? [R=301,NE,NC,L]

RewriteCond %{HTTP_HOST}  ^france-property\.frenchentree\.com$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^advertise-with-us(.*)$ http://www.frenchentree.com/property-for-sale/advertise-with-us? [R=301,NE,NC,L]

RewriteCond %{HTTP_HOST}  ^france-property\.frenchentree\.com$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^favourites(.*)$ http://www.frenchentree.com/property-for-sale/favourites? [R=301,NE,NC,L]

RewriteCond %{HTTP_HOST}  ^france-property\.frenchentree\.com$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^saved-searches(.*)$ http://www.frenchentree.com/property-for-sale/saved-searches? [R=301,NE,NC,L]

RewriteCond %{HTTP_HOST}  ^france-property\.frenchentree\.com$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^system(.*)$ http://www.frenchentree.com/property-for-sale/system$1? [R=301,NE,NC,L]

RewriteCond %{HTTP_HOST}  ^france-property\.frenchentree\.com$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^$ http://www.frenchentree.com/property-for-sale/? [R=301,NE,NC,L]

I have run this through the winginx convertor, which has output the following:

# nginx configuration
location / {
if ($http_host ~* "^france-property\.frenchentree\.com$"){
rewrite ^/for-sale/(.*)$ http://www.frenchentree.com/property-for-sale/search/$1? redirect;
}
if ($http_host ~* "^france-property\.frenchentree\.com$"){
rewrite ^/for-sale$ http://www.frenchentree.com/property-for-sale/search? redirect;
}
if ($http_host ~* "^france-property\.frenchentree\.com$"){
rewrite ^/for-sale-details(.*) http://www.frenchentree.com/property-for-sale/details$1? redirect;
}
if ($http_host ~* "^france-property\.frenchentree\.com$"){
rewrite ^/buyer-registration(.*)$ http://www.frenchentree.com/property-for-sale/buyer-registration? redirect;
}
if ($http_host ~* "^france-property\.frenchentree\.com$"){
rewrite ^/advertise-with-us(.*)$ http://www.frenchentree.com/property-for-sale/advertise-with-us? redirect;
}
if ($http_host ~* "^france-property\.frenchentree\.com$"){
rewrite ^/favourites(.*)$ http://www.frenchentree.com/property-for-sale/favourites? redirect;
}
if ($http_host ~* "^france-property\.frenchentree\.com$"){
rewrite ^/saved-searches(.*)$ http://www.frenchentree.com/property-for-sale/saved-searches? redirect;
}
if ($http_host ~* "^france-property\.frenchentree\.com$"){
rewrite ^/system(.*)$ http://www.frenchentree.com/property-for-sale/system$1? redirect;
}
if ($http_host ~* "^france-property\.frenchentree\.com$"){
rewrite ^/$ http://www.frenchentree.com/property-for-sale/? redirect;
}
}

I'm sceptical of the above for two reasons... 1. because the subdomain is hosted on another server, and therefore I suspect I will need a server level redirect, something like this:

server {
   server_name france-property.frenchentree.com;
   rewrite ^/for-sale/(.*) $scheme://www.frenchentree.com/property-for-sale/search/$1 permanent;
   rewrite ^/for-sale-details(.*) $scheme://www.frenchentree.com/property-for-sale/details$1 permanent;
   rewrite ^/buyer-registration(.*) $scheme://www.frenchentree.com/property-for-sale/buyer-registration$1 permanent;
   etc
   etc
   etc

}

And 2. because I understand that if statements shouldn't be used in nginx, if it can be avoided.

Can someone please point me in the right direction, if only for one example which I can then replicate? I would usually just do some tests, however, the websites are live and functional, and are quite popular so would like to get it right first time!

Many thanks in advanced!

Cheers

Matt

1
  • I don't understand because the subdomain is hosted on another server, and therefore I suspect I will need a server level redirect - but it's much better to use multiple server blocks with a server name and specific rules rather than one server block and if ($http_host ~* checks. I would usually just do some tests, however, the websites are live and functional - you wouldn't usually run tests on the live server would you =)? Use a test install.
    – AD7six
    Nov 27, 2014 at 12:51

1 Answer 1

1
  1. Avoid at all costs automated tools which lack, by definition, the brains to properly convert rules. There is no simple 1-to-1 match between Apache and nginx ways. They have 2 different mindset. You thus need to understand what you are doing and use the nginx way of configurating it. That is IMHO a complex problem out of the reach of an automated tool.

  2. Concerning rules:

    1. RewriteCond %{HTTP_HOST} is related to host matching. On the nginx side it is done through server_name. The syntax might be different: check the docs.
    2. RewriteCond %{QUERY_STRING} ^$ means you explicitely want the QUERY_STRING value to be empty. If that is mandatory to you, use an if block matching against $args being empty. That is an exception because I cannot see a better way of doing it: if should be avoided as much as possible. You should put the if block in a location which isolates the right URI.
    3. RewriteRule can either be replaced by a combination of location and return (preferred way) or rewrite (which is to be avoided as much as possible). When you use location, try to use prefix locations as much as possible, since regex locations evaluation is indeed slower. Based on performance, it is thus best to avoid them whenever possible too.

To get advice on getting a clean and scalable configuration, listen to nginx inventor who hosted a talk at the nginx.conf user conference: Scalable configuration with nginx

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .