1

I am struggling with NginX rewrites and redirects, but with only half success. Would appreciate some help.

Situation:

Customer has 4 domains, which they want to point to all to same Wordpress installation. Ok, I found nice WP Plugin (Multiple Domain) to accomplish that.

Previous WP install was inside /wp/ folder, which I had to move to root to be able for Multiple Domains plugin to work.

So I had to add NginX options to deal with previous /wp/ sub-folder for two reasons:

  • SEO and Google indexes all still point to /wp/ URLs, which would be dead without below rewrite
  • Web site designer hard-coded some links to images and some other things with /wp/ included in URL, so the below rewrite deals with this problem, too

Now ALL belwo works, and the best part is, that URL keeps the domain as it was entered (does not redirect to main domain):

domainA.com

domainB.com/?cat=7 --> domainB.com/news

domainC.com/wp/something --> domainC.com/something

location ^~ /wp/(.+) { rewrite ^/wp/(.+)$ $1 last; }

Problem? YES, all is working fine, EXCEPT when /wp/ is called without parameters:

domainA.com/wp --> 404

domainB.com/wp --> 404

...

I also tried (.*), but did not help. Ideas?

EDIT As it is configured now, all 4 domains work OK and fine with /wp/ in the middle of URL string, like this:

domain.com/wp/news --> domain.com/news (this is great!)

The only problem persists, when /wp is at the end:

domain.com/wp --> domain.com/wp (does NOT rewrite/redirect, which is NOT ok)


EDITED MORE CLEAR QUESTION

I could just simply ask for NginX rewrite rule, which would include all these locations:

domain.com/wp/something
domain.com/wp/wp-config
domain.com/wp/?cat=7
domain.com/wp/
domain.com/wp

...but would NOT include wp* files:

domain.com/wp-config

This works, but does not cover /wp and /wp/ alone, without parameters:

location ^~ /wp/(.+) {
 rewrite ^/wp/(.+)$ $1 last;
}

1 Answer 1

1

Your regex mandates the trailing slash after /wp to be present to match, but you say that you don't want that.

location ^~ /wp/(.+) {
     rewrite ^/wp/(.+)$ $1 last;
}

So just make the trailing slash optional. For that matter you don't really need the location; you can just place the rewrite directly in the appropriate server block.

rewrite ^/wp(/.+)? $1 last;
4
  • thanx, but your hint is not working. Maybe because I only have NginX OPTIONS to add via ISPConfig control panel?
    – Labsy
    Sep 27, 2018 at 20:02
  • If I remove trailing slash from directive rewrite ^/wp/(.+)$ $1 last; and make it optional per your suggestion into rewrite ^/wp(/.+)$ $1 last;, then all redirections from domain.com/wp/something to domain.com/something do not work anymore. I also tried with or without location declaration.
    – Labsy
    Sep 27, 2018 at 20:16
  • @Labsy Oops, I forgot a required bit in the regex. Please see the updated answer. The ? makes the bit in () optional. Sep 28, 2018 at 16:53
  • Michael - rewrite alone produces looped rewrite and 500 error. Will try with location...and nope. Will probably need two different redirections...
    – Labsy
    Sep 28, 2018 at 20:27

You must log in to answer this question.

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