Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 *

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

However, i'd like to do something funky with subdomains... so I have and 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!


share|improve this question
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 '11 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 '11 at 22:04

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 {

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

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

  rewrite ^ /index.php?/$SUB/$request_uri last;
share|improve this answer

You can change your rule to

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

but unfortunately that gets you "paths" like '/index.php?/', 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, but none other.

share|improve this answer
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 '11 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 '11 at 22:16
@user18404: You can, it's even in the main nginx introduction: 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. – Martin Fjordvald Jun 11 '11 at 2:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.