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