Nginx redirect top level domain - nginx

I've just started using Nginx instead of apache.
I'm trying to find a similar way to do something like this, in Apache:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
But in Nginx.
So basically any .co.uk traffic gets redirected to .com

You could create a separate server block for the .co.uk domain:
server {
listen 80;
server_name .example.co.uk;
return 301 $scheme://www.example.com$request_uri;
}
See this document for details. Note that the default_server option can also be used to match any non-specific domain name.

Related

Rewrite page url as subdomain in apache htaccess

I have a lot of page URLs like domain.com/page I want a rewrite rule that will change all my pages URLs as page.domain.com meaning whatever will come after the domain just rewrite it as a subdomain
example urls
expertpro.cloud/hot-to-write-blog to hot-to-write-blog.expertpro.cloud
expertpro.cloud/game to game.expertpro.cloud
expertpro.cloud/nibm-full-form to nibm-full-form.expertpro.cloud
expertpro.cloud/choclate to choclate.expertpro.cloud
expertpro.cloud/harmony-in-life to harmony-in-life.expertpro.cloud
expertpro.cloud/paki-cold-places to paki-cold-places.expertpro.cloud
expertpro.cloud/you-are-one to you-are-one.expertpro.cloud
I already have some code for Nginx
The empty location = / block is necessary so that you don't redirect
http://example.com/ to http://.example.com/.
//replacing domain name in rewrite rule
location = / {
# don't redirect $server_name/
}
location / {
rewrite ^\/([^\/]*).*$ https://$1.$server_name/ redirect;
OK, all you need is an internal rewrite, as it looks:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [END]
You obviously need the rewriting module to be loaded into your http server.
That would be a variant which additionally redirects direct requests to the internal URL:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^/?([^/]+)(/.*)?$ https://$1.example.com$2 [R=301]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [END]
Here it makes sense to start out with a R=302 temporary redirection and only to change that into a R=301 permanent redirection once everything works as expected.
In general you should try to implement such rules in the actual http server's host configuration. If you have no access to that you can instead use a distributed configuration file (".htaccess"), but those come with disadvantages. You need to enable the interpretation of such files in that case.

Nginx URL masking to another domain

I just installed Nginx as a feature of VestaCP on my VPS.
What I'm trying to do is to mask the URL http://example.com to http://12345.com, so when users visit http://example.com/path/file.mp4 they will see the content of http://12345.com/path/file.mp4 but browsers still show the URL http://example.com/path/file.mp4.
I googled and found a topic here. It looks like the answer I'm looking for. However when I applied his code to nginx.conf, VestaCP showed Error: nginx failed to start with new config and stopped working.
Here is the code:
server {
listen 80;
server_name sub.example.com;
location / {
proxy_pass https://123.12.12.12;
rewrite ^/$ /path last;
}
}
Is this the right solution for me? I'm completely new to this so I don't know if I did it correctly.
Edited: I was able to get the job done using apache .htaccess. How do I convert this to use with Nginx?
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^ http://12345.com%{REQUEST_URI} [L,NE,P]
You just need below, no rewrite needed
server {
listen 80;
server_name sub.example.com;
location / {
proxy_pass https://123.12.12.12;
}
}

Laravel with Wordpress blog (with WP API)

My progect structure
/root_dir
/app
/bootstrap
...
/public
index.php
.htaccess
/wp #wordpress installation here
/wp-admin
/wp-content
/wp-includes
index.php
.htaccess
...
So what I want to acomplish is to have Laravel project with views, API etc
And I need wordpress only for one route domain.com/blog where I'll have separate default WP blog that doesn't integrate or interact with my Laravel app in any way
My /public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_URI} !^(blog) #ADDED ONLY THIS MAGIC LINE
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
And my /public/blog/.htaccess is WP default
And it works like a charm!
My lara routes are fine... API routes - good... WP blog - great...
BUT
Now I want to call WP API, e.g. domain.com/blog/wp-json/wp/v2/posts
And all I see is Laravel's "NOT FOUND" page
Please help!
I have a strong feeling it can be solved with .htacess 'magic' but unfortunately I'm noob in that...
P.S. Found a similar question here Laravel project next to Wordpress project (in public_html folder)
But I need vice versa solution
you can proxy pass the address http://example.com/blog to some other address,
for example using nginx :
server {
listen 80;
server_name blog.example.com;
location / {
root /var/www/wordpress;
try_files $uri /index.html;
}
}
server {
listen 80;
server_name example.com www.example.com;
location / {
root /var/www/laravel;
try_files $uri /index.html;
}
location /blog/ {
proxy_pass http://blog.example.com/;
}
}
This is a simple and also clean way, and also you don't need to change even one line of code to have laravel and wordpress aside each other.
Holy cow!
The solution was to navigate to dashboard/settings/permalinks and change option from 'plain' to any other!
Found the answer here https://wordpress.org/support/topic/rest-api-in-wp-4-7/#post-8620246
You need to be using pretty permalinks to access /wp-json/. If you’re not, you can instead use ?rest_route=/ to get the index and ?rest_route=/wp/v2/posts (e.g.) for specific routes.

WordPress nginx redirect from non www to ssl www

Ubuntu 16.04,
Nginx 1.11.6,
php 7.0.8
I know there are several questions regarding this, but I am still having problems.
Also this is not a problem regarding a loop with the admin site, not yet anyway.
I am using a simple 301 redirect that works perfectly in non WordPress sites
##redirect to www and ssl
return 301 https-://www.mydomain.com$request_uri;
However, this does redirect to ssl but without the www
Also
In Setting > General > WordPress Address and Site Address are both set to
http-://domain.com
If I change them to http-://www.domain.com everything works, however I eventually need this to be a multi-site in which case they recommend only using the domain name.
I also have block just for ssl, again all this works as expected in non WordPress site.
server {
listen x.x.x.x:443 http2;
server_name www.domain.com;
I think you must have to rewrite your redirection code.
Prepare your code in following format.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Nginx: I cannot get a 301 redirect from the Query Paramaeter to the Path

I would like to redirect:
From:
http://www.fascinate.jp/index.php?option=com_virtuemart&page=shop.browse&manufacturer_id=4&Itemid=62&limitstart=0&lang=en
To:
https://www.fascinate.jp/english/brand/devoa/
The following is the redirect settings for my current site:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{QUERY_STRING} (^|&)option=com_virtuemart&page=shop\.browse&manufacturer_id=4&Itemid=62&limitstart=0&lang=en($|&)
RewriteRule ^index\.php$ https://www.fascinate.jp/english/brand/devoa/? [L,R=301]
How should I rewrite the Apache Settings so I can use it with Nginx?
The version of Nginx I am using is nginx/1.9.11.
The second rewrite condition translates directly to an if block. The actual rewrite translates to a return 301 in its simplest form. Like this:
if ($args ~ "(^|&)option=com_virtuemart&page=shop\.browse&manufacturer_id=4&Itemid=62&limitstart=0&lang=en($|&)") {
return 301 https://www.fascinate.jp/english/brand/devoa/;
}
nginx is not good at implementing multiple conditions. The RewriteCond %{SERVER_PORT} 80 will only cause problems if you serve both SSL and non-SSL from the same server block. If you can separate out non-SSL into its own server block, then the above if block can be applied only to the non-SSL URLs.
See this and this for more. And of course this.

Resources