Configuring nginx to work with WordPress Multisite - 301 redirect - wordpress

I am having issues with trying to get a WordPress Multisite (using subdomains) running on nginx.
Here is my ideal set-up:
domain.com
sub1.domain.com
sub2.domain.com
sub3.domain.com
Any requests to www.domain.com should be 301'd to domain.com (my understanding is that WordPress strips out www anyways?)
I am not wanting to access any of the subdomains by any other means, e.g domain2.com would direct a user to sub2.domain.com - this isn't what I want.
I have followed this DigitalOcean tutorial and everything has worked fine with regular WordPress installs.
My local hosts file is set up as follows:
XXX.XXX.XXX.XXX domain.com
XXX.XXX.XXX.XXX sub1.domain.com
etc...
My issue is that whenever I attempt to visit either domain.com or sub*.domain.com, I am redirected to a completely different site on my server, the one which just so happens to be the top of the list in sites-enabled. On closer inspect, when checking the Network tab in Chrome, I can see that my request for domain.com or sub*.domain.com is 301 redirected to the aforementioned unrelated site.
I have flushed my local Chrome cache, am constantly using Incognito mode and I just cannot shake this redirect. I'm even using Firefox as I rarely use it so wouldn't expect any caching whatsoever for domain.com
I may be completely off the mark here and it could be nothing to do with the browser at all but rather the server and nginx configuration. Something is forcing a 301 and I don't know what.
It seems that this plugin comes up a lot in discussion but as far as I can tell, this is only applicable when wanting to map a regular domain to a subdomain, which isn't what I want.
Can anyone please offer any advice/solutions as to how I can configure so everything is routed correctly? My local environment runs a LAMP stack so I have had this working and I have altered the wp-config.php file to reflect the new 'live' domain (remember, I have not configured DNS, I have just hacked around in etc/hosts.
Any help much appreciated as always :)

The only thing that avoids your request to be served by the "fallback", default vhost (that is, the "first" there) is the server_name directive.
Just by specifying location blocks in multiple server blocks with the same (or no) server_name won't make them magically match.
You can however use regular expression server names.
See here for how.
For the specific case with only a single domain, you can use
server_name *.whatever.com;
then match location with return 301 http://whatever.com; for subdomains only. That way you will not need regular expression server names.
Note that you should have server_name for the default server, like
server_name "_";

So in the end I decided to build the multisite from scratch in my nginx environment as opposed to trying to take what worked in Apache and re-configure for nginx. As far as I can tell, I had updated everything that you would expect to have to do so from Apache to nginx as well as updating local domains to live ones in the database and wp-config.
While this approach didn't strictly 'fix' my issue, I do feel like I've learned a valuable lesson here - sometimes starting from scratch is better than trying to make something fit!

Related

Using 301 redirects at server config level on NGINX to avoid suspected WP issues?

OK, our old website is Wordpress installed on a NGINX bitnami distribution in AWS.
Our new website is on managed hosting on a new domain name.
For the migration, we'd used the plugin 'Simple 301 redirects' to map each page to the new site structure.
However, for reasons we can't fathom and have only just spotted, all the redirects are reporting as 302 - which is hurting our SEO.
Our theory is that re-implementing the 301s but at server level config on the NGINX server will be used before it hits the WP installation, and hopefully achieve an actual 301 redirect.
I can find a number of support articles that might help us: https://www.digitalocean.com/community/tutorials/how-to-create-temporary-and-permanent-redirects-with-nginx although this article suggests using return instead of rewrite: https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/?highlight=rewrite#taxing-rewrites
If our theory is correct, we'll create a new server config that redirects each individual page (we need to do this, as the site structure has changed in some cases - it is about 250 pages FYI).
Would somebody be able to offer any additional advice on this?
We're still baffled as to why it is returning 302 redirects rather than 301...!
Greatly appreciated!

Multiple sites on same server and issues with SSL

A number of months ago I created the site cascocc.com. All is well. I then created another site coloradospringshomebuilders.com. All was well, I started development.
I then got an SSL for cascocc.com and installed it, all was well. Some items were not being delivered using SSL so I used the plug in force SSL to get the fancy green bar to show up.
Now coloradospringshomebuilders.com redirects to cascocc.com and I can not figure out how to fix it. I have reverted all my htacess files to the most basic possible. Am I missing one or what am I missing?
Any ideas?
EDIT: I deleted ALL .htaccess files on the entire server. The second site is still redirecting to the first and trying to use the firsts site SSL cert. I do not understand how this is happening
1st sites .htaccess file
2nd sites .htaccess file
This problem sounds like your Virtual hosts are not setup correctly. The SSL setting is overriding any other setting and redirecting to the page that is setup with SSL.
That can only be solved in the server settings (in this case most likely the virtual host). Depending on where you host your sites you might not have access to this, so I would advise to direct this question to your hosting provider.
This whole "redirect" is happening before the server reads the .htaccess files, that's why you can't solve this problem there.

Https on Elastic Beanstalk - wordpress

I have an annoying issue with https on a wordpress website running on aws eb single instance.
I have the same website in a vps, https works fine, styles and css are served via https. Just to be sure (I've spent 2 days trying to figure out what's wrong) few minutes ago I deployed on eb a copy of the website - editing only the database settings.
So, the issue is that in eb if I go to https://mywebsite.com the page is "broken", styles and scripts are server through http.
In the vps if I go to https://mywebsite.com styles and scripts are https://mywebsite.com/styles/style.css, in eb styles and scripts are http://mywebsite.com/styles/style.css.
Why is this happening and how can I fix that?
I've tried edit the wp_options table and set siteurl and home to https://mywebsite.com (the same website in the vps has values set to http://mywebsite.com)
AWS Elasticbeanstalk single instance Force SSL Redirect loop
htaccess (to force https or to force only http) is simply ignored, I've tried so many solutions but no one worked. The only solution that works for me is a php redirect to http after checking if the server variable http_x_forwarded_server is set
I've read that all the https connections - in eb single instance - through port 443 are "proxied" to port 80 (that could be the problem?)
So, any solutions? I don't want to force https in the website, so I don't want to install plugins that forces https, I just want that the website doesn't broke when navigating the website through https
Just after adding the post, I've managed to solve this problem.
For future references, the solutions is to add this variable $_SERVER['HTTPS'] and set it to 'on'.
This is my code
if(isset($_SERVER['HTTP_X_FORWARDED_SERVER'])) {
$_SERVER['HTTPS'] = 'on';
}
You can add it to index.php, or as I did, in the header.php of your theme (in this case be aware that if you change theme that code will not executed)

lots of redirect chains WordPress site on ngix + php fpm

until I added the string: remove_filter('template_redirect', 'redirect_canonical'); to functions.php of current theme the site even did not load in browser. Now it loads ok, but when I checked site load with tools.pingdom I saw dozens of those "Remove the following redirect chain if possible" errors. Each of them is the same error about some image:
Remove the following redirect chain if possible:
http://www.domain.com/wp-content/thumbs/fgd/0-9/10-bullets.png
http://domain.com/wp-content/thumbs/fgd/0-9/10-bullets.png
etc. about 200 of the same kind.
It is a game arcade plugin and those are paths to thumbnails of games. Could you point me where to dig?
Ok, it seems that you have (or used to have) your wordpress site configured at the url www.domain.com
This means that any images you pasted on your posts began with www.domain.com.
It's possible that images on your theme are also hard linked to www.domain.com too.
Your nginx vhost has these rules
server_name www.domain.com domain.com;
if ($host != 'domain.com') {
rewrite ^/(.*) http://domain.com/$1 permanent;
}
Which mean "listen to either www.domain.com or domain.com, but force whoever request the www domain to perform another request in order to end up at domain.com."
This is meant to canonicalize your site url and avoid google indexing your content twice, which would split pagerank and authority and probably downrate it as duplicate content.
The problem is that this rule operates on the images you already have linked ar www.domain.com, so everytime you request them, nginx redirects you.
TL/DR
Make sure the url you have configured at your wordpress installation matches the one you're enforcing in your nginx vhost. If you want to strip the www part, fine, but do it in both parts.
Second: for the images on your theme, you'll have to manually edit the urls to get rid of the www. You can also use relative links
<img src="/wp-content/thumbs/fgd/0-9/10-bullets.png">
instead of
<img src="http://domain.com/wp-content/thumbs/fgd/0-9/10-bullets.png">
Third: do the same check for the posts you have published until now.

Host blog on different server?

Currently I have my companies blog on the same server as our main web app - as in example.com/blog
What would be the easiest way to move the blog to a different server yet still let users either access the blog at example.com/blog or blog.example.com?
Issue 301 HTTP redirects from website.com/blog to blog.website.com
The specifics depend on the server.
The answer that I provide is assuming that you are using Apache as the webserver as this is what I am familiar with. If you are using IIS then you may be able to find a similar solution but I cannot guarantee this.
In the past I have come across similar issues, whereby a client is hosting their main website at www.mydomain.com but their blog is hosted by their marketing guys at blog.mydomain.com. For SEO purposes it ca be beneficial to have the blog appear at www.mydomain.com/blog although this is not physically possible due to the nature of DNS protocols.
The answer is to use an Apache module called mod_proxy which allows you to map the url /blog to a subdomain and this is invisible to the end-user and, more importantly, search engines. CodeHaus have published a good article on Configuring mod_proxy (update 2016: link leading nowhere) and you may like to have a read.
mod_proxy is quite complex and it can take a while to get the settings correct but it is by far the best solution to the problem that you describe.
You can use a simple javascript redirect code such as the one below to redirect the user.
<script type="text/javascript">
<!--
window.location = "http://www.example.com/";
//-->
</script>
Are you hosting these servers in your own data center, or are you purchasing hosting? Our corporate site is hosted on multiple servers in our data center. We use load balancers to route traffic to different server farms based upon the URL. So
http://www.foo.com/assets/* might route to a farm of Apache HTTP servers that serve nothing but image files while
http://www.foo.com/apps/* might route to a farm of JEE application servers and
http://www.foo.com/services/* might route to a farm of servers especially designed to host web services and
http://www.foo.com/blog/* might route to a farm of servers that handle nothing but WordPress.
Works great... if you have all of this at your disposal. If you don't, then check out mod_proxy like Matt said (which we used before we bought load balancers).
blog.example.com
blog.example.com is easiest, since you can simply point it to a different IP address.
That is not what I want for a project I'm working on.
example.com/blog
Instead I use Apache mod_proxy to point /blog to another server. Apache docs here.
This worked well:
ProxyPass /blog http://blog.example.com/
ProxyPassReverse /blog http://blog.example.com/
But only after the proxy modules were loaded correctly:
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
LoadModule proxy_html_module /usr/lib/apache2/modules/mod_proxy_html.so
LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so
Notes
Because of CloudFlare I also had to host the blog from a different hostname, not *.example.com.
I added Disallow: / to my blog.example.com/robots.txt. Like this search engines will not start indexing in two locations. And search engines will simply ignore blog/robots.txt.

Resources