2

I'm pretty new to nginx and am trying to setup some pretty simple rewrite rules, but they don't seem to be working.

Just for your info, this server { .. } has its server_name domain.com *.domain.com.

I have this rewrite ^ /index.php?/$request_uri; which seems to work great. This will match domain.com/asd/sad/sad and everything works as it should.

However, i'd like to do something funky with subdomains... so I have s1.domain.com and s198.domain.com but I would like to change my rewrite so it goes to /index.php?/s1/$request_uri if that makes sense? So the subdomain goes in before the $request_uri.

This is what I have tried so far:

if ($host ~* ^([a-z]+)\..*) {
    rewrite ^(.*)$ /index.php?/$1/$request_uri;
}
rewrite ^ /index.php?/$request_uri;

Really appreciate any help you can give!

Thanks.

2
  • Interesting method of parameterizing the path -- I would have gone for something like /index.php?q=$request_uri, but if this is what works for you...
    – Kromey
    Jun 10, 2011 at 22:02
  • I'm using FuelPHP and the site is currently running under Apache2 with this RewriteRule RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{HTTP_HOST} ^dev\..* RewriteRule ^(.*)$ index.php/dev/$1 [L]
    – tarnfeld
    Jun 10, 2011 at 22:04

2 Answers 2

1

The first of all: use of if is discouraged by Nginx author for a very good reason: it has very nasty side-effects. You may still use it in simple use-cases, but using if and rewrite simultaneously is like a bomb.

Your use case can be easily implemented using two server sections:

server {
  server_name domain.com;

  rewrite ^ /index.php?/$request_uri last;
}

server {
  server_name ~ ^(?<SUB>.+)\.domain.\com$ ;

  rewrite ^ /index.php?/$SUB/$request_uri last;
}
0

You can change your rule to

rewrite ^ /index.php?/$host/$request_uri;

but unfortunately that gets you "paths" like '/index.php?/s1.domain.com/asd/sad/sad', not '/index.php?/s1/asd/sad/sad' as you appear to be after.

Can you simply use the $_SERVER['HTTP_HOST'] variable in your PHP script and figure out the appropriate change within your script itself instead of making nginx do all the work?

Edited to add:

Wrote this before you posted your comment about FuelPHP; don't know what that is, but I presume it means you're either unable or unwilling (not that that's a bad thing) to make a change to the script as I suggested. Although the Apache rule you posted there doesn't automatically do what you're asking for any given subdomain, either -- it works for dev.domain.com, but none other.

3
  • FuelPHP is a framework :) I'm using its awesome HMVC functionality here, they allow you to separate out controllers/views/models into a modular system and then they are all namespaced under /module/controller/foo/bar. I was hoping for nginx to be able to grab the first segment of the domain and use that as the module...
    – tarnfeld
    Jun 10, 2011 at 22:16
  • Since I still use apache for local development and staging, I don't want to have to modify any application code for this to work.
    – tarnfeld
    Jun 10, 2011 at 22:16
  • @user18404: You can, it's even in the main nginx introduction: nginx.org/en/docs/http/server_names.html Also. Remember to use the fastcgi_split_path_info directive and turn off cgi.fix_pathinfo in PHP so that you aren't likely to be vulnerable to arbitary code execution. Jun 11, 2011 at 2:39

You must log in to answer this question.

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