# Rewrite help please

**URL:** <https://www.ruby-forum.com/t/rewrite-help-please/226484>\
**Category:** NGINX\
**Created:** [December 9, 2012, 7:06am UTC](https://www.ruby-forum.com/t/rewrite-help-please/226484 "2012-12-09T07:06:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![steveybaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steveybaby](https://www.ruby-forum.com/u/steveybaby)\
**Post date:** [December 9, 2012, 7:06am UTC](https://www.ruby-forum.com/t/rewrite-help-please/226484/1 "2012-12-09T07:06:31Z")

</div>

I’ve got a WP site that also provides landing pages for a number of  
other sites. I’ve set it up as the default server config for that IP,  
and that’s working fine. However, the requirement I’ve got is to go to a  
specific landing page dependant on domain name. This is how I’ve gone  
about it ( as there are a lot of pages…

map $http\_host $page\_redirect {  
hostnames;

default notset;

.example1.com /link/example1;  
…  
}

hostname [example.com](http://example.com);

location = / {  
if ( $page\_redirect ~ notset ) {  
rewrite ^ /index.php break;  
}

# this one works but rewrites the url.

rewrite ^ $page\_redirect redirect;  
#try\_files $page\_redirect $page\_redirect/ /index.php?$page\_redirect;  
}

So I’m basically only trying to redirect from [http://www.example.com](http://www.example.com) for  
example, but not [http://www.example.com/index.php](http://www.example.com/index.php), and only from  
predefined domains.

As the comment says, this does work if I use a rewrite … redirect; but  
the URL then changes to [http://www.example1.com/link/example1](http://www.example1.com/link/example1), whereas I  
want to see [http://www.example1.com](http://www.example1.com) alone.

The try\_files comes up with a 404, which is really perplexing… I  
expected it to go through the .php block like the others!

Any ideas what I’m doing wrong?

Cheers,

Steve

---

<div class="post-metadata">

**Author:** ![steveybaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steveybaby](https://www.ruby-forum.com/u/steveybaby)\
**Post date:** [December 9, 2012, 11:29am UTC](https://www.ruby-forum.com/t/rewrite-help-please/226484/2 "2012-12-09T11:29:55Z")

</div>

On Sun, Dec 09, 2012 at 07:06:01PM +1300, Steve H. wrote:

Hi there,

> I’ve got a WP site that also provides landing pages for a number of  
> other sites. I’ve set it up as the default server config for that IP,  
> and that’s working fine.

The usual nginx way is to have a different configuration for each  
server\_name in different server{} blocks, which is usually based on the  
Host: header sent by the browser.

You want slightly different handling for different Host: headers, but  
all in the same server{} block. That’s ok, but when you start adding  
more special cases, you should probably consider switching to multiple  
server{} blocks.

> However, the requirement I’ve got is to go to a  
> specific landing page dependant on domain name. This is how I’ve gone  
> about it ( as there are a lot of pages…
> 
> map $http\_host $page\_redirect {

Some small points:

$http\_host is “whatever the browser sent, including :80 if it did  
that”. $host is “the hostname part of that, apart from a couple of  
exceptions”. If you’re not sure which to use, use $host.

“redirect” usually suggests “send a http 301 to the browser, so that  
it issues a new request”. That’s not what you want to happen. Perhaps  
$landing\_page would be clearer for the next person to read? But that’s  
not directly relevant to the configuration issue.

> hostnames;
> 
> default notset;
> 
> .example1.com /link/example1;

When you have many things in the one server block, consistency is great.

“notset” is a flag which means “do something special”.

What is “/link/example1”? A file that should be served? A file that  
should be php-processed? A directory containing a file that should be  
php-processed?

Maybe something like

default /index.php;  
.example1.com /link/example1/index.php;

would allow you to handle all situations equivalently.

> }
> 
> hostname [example.com](http://example.com);
> 
> location = / {  
> if ( $page\_redirect ~ notset ) {  
> rewrite ^ /index.php break;  
> }

location → if → “return …” is OK.  
location → if → “rewrite … last” is OK.  
location → if → anything else, and you’re on your own. It’s  
consistent,  
but not necessarily easily predictable what will happen.

> # this one works but rewrites the url.
> 
> rewrite ^ $page\_redirect redirect;

Yes - “redirect” means “rewrite the url”.

> #try\_files $page\_redirect $page\_redirect/ /index.php?$page\_redirect;  
> }
> 
> So I’m basically only trying to redirect from [http://www.example.com](http://www.example.com) for  
> example, but not [http://www.example.com/index.php](http://www.example.com/index.php), and only from  
> predefined domains.

Given your above configuration, am I right that what you want is:

[http://www.example.com/](http://www.example.com/) → php-process /usr/local/nginx/html/index.php  
[http://www.example.com/index.php](http://www.example.com/index.php) → php-process  
/usr/local/nginx/html/index.php  
[http://www.example.com/file.png](http://www.example.com/file.png) → send /usr/local/nginx/html/file.png  
[http://www.example1.com/](http://www.example1.com/) → php-process  
/usr/local/nginx/html/link/example1/index.php  
[http://www.example1.com/index.php](http://www.example1.com/index.php) → php-process  
/usr/local/nginx/html/index.php  
[http://www.example1.com/file.png](http://www.example1.com/file.png) → send /usr/local/nginx/html/file.png

where in each case “send” or “php-process” means “respond http 200 with  
the output of that file”?

> As the comment says, this does work if I use a rewrite … redirect; but  
> the URL then changes to [http://www.example1.com/link/example1](http://www.example1.com/link/example1), whereas I  
> want to see [http://www.example1.com](http://www.example1.com) alone.
> 
> The try\_files comes up with a 404, which is really perplexing… I  
> expected it to go through the .php block like the others!

After “location → if → anything else”, I find it’s usually not worth  
wondering why things do or don’t work as hoped. You’re in “here be  
dragons” territory anyway. That’s nginx.

> Any ideas what I’m doing wrong?

With the assumptions and changes from above, maybe

location = / {  
fastcgi\_pass unix:php.sock;  
include fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$page\_redirect;  
}

is closer to what you want?

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![steveybaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steveybaby](https://www.ruby-forum.com/u/steveybaby)\
**Post date:** [December 9, 2012, 9:51pm UTC](https://www.ruby-forum.com/t/rewrite-help-please/226484/3 "2012-12-09T21:51:32Z")

</div>

On Sun, 2012-12-09 at 10:29 +0000, Francis D. wrote:

> where in each case “send” or “php-process” means "respond http 200 with  
> wondering why things do or don’t work as hoped. You’re in "here be  
> }
> 
> is closer to what you want?
> 
> Good luck with it,

Thanks for the ideas Francis, it’s cleaned up my config no end!

This is a WordPress site, so all requests have to go to /index.php to be  
processed. The URL dictates the page returned.

I can’t seem to find a way of mimicing what I’m trying to do with your  
method, no matter what fastcgi\_params I try setting up.

Cheers,

Steve

---

<div class="post-metadata">

**Author:** ![steveybaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steveybaby](https://www.ruby-forum.com/u/steveybaby)\
**Post date:** [December 9, 2012, 10:06pm UTC](https://www.ruby-forum.com/t/rewrite-help-please/226484/4 "2012-12-09T22:06:43Z")

</div>

On Mon, 2012-12-10 at 09:51 +1300, Steve H. wrote:

> On Sun, 2012-12-09 at 10:29 +0000, Francis D. wrote:

> > Good luck with it,
> 
> Steve

As with everything like this, 2 extra minutes with google found me the  
answer. I need to add

fastcgi\_param PATH\_INFO $page\_redirect;

and it works perfectly.

Chanks for putting me on the right track,

Steve

---

<div class="post-metadata">

**Author:** ![steveybaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steveybaby](https://www.ruby-forum.com/u/steveybaby)\
**Post date:** [December 10, 2012, 8:51pm UTC](https://www.ruby-forum.com/t/rewrite-help-please/226484/5 "2012-12-10T20:51:36Z")

</div>

On Mon, Dec 10, 2012 at 10:05:58AM +1300, Steve H. wrote:

> On Mon, 2012-12-10 at 09:51 +1300, Steve H. wrote:

Hi there,

> fastcgi\_param PATH\_INFO $page\_redirect;
> 
> and it works perfectly.

“works perfectly” is always a good report. Glad you found what you  
wanted.

All the best,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
