Rewrite help when files do NOT have a ".php" extension

António P. P. Almeida appa at perusio.net
Wed Jun 1 17:06:20 MSD 2011


On 1 Jun 2011 13h00 WEST, nginx-forum at nginx.us wrote:

> Loving Nginx so far! So much faster and leaner than Apache. 
>
> Apologies for this yet another Rewrite rule help after so many
> threads, but one of domains has a special need and I am struggling
> with Rewrite rules.
>
> The logic is simple: 
>
> 1. For anything in /site folder, it should treat the static files as
>    it
> is, but all else is php (file names don't have .php extension). Still
> further in this folder:  
>
> a. Within /site/private, it should be password protected
> b. Within /site/wordpress, there is a wordpress blog 
>
> 2. For the main folder  "/" all URLs should be directed to
> "/site/redirect". 
>
>
>
> Following is in URI examples, so that it easier to visualize: 
>
> (Lines starting with asterisks ** are already working)...
>
>
>
> /abc             --->  /site/redirect?u=abc
> /xyz             --->  /site/redirect?u=xyz
>
> **  /site/abc        --->  /site/abc  (as PHP file)
> **  /site/xyz        --->  /site/xyz  (as PHP file)
>
> ** /site/1.gif      --->  Served as it is, static 
> ** /site/2.png      --->  Served as it is, static 
>
> ** /site/private/abc  --->  Inside http auth, serve as PHP 
> /site/private/   --->  Inside http auth, serve as PHP
> /site/private/index
>
> /site/wordpress/..  ---> This is the usual wordpress thing.. 
> The apache rules used to be: 
> <IfModule mod_rewrite.c>
> RewriteEngine On
> RewriteBase /site/wordpress/
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_FILENAME} !-d
> RewriteRule . /site/wordpress/index.php [L]
> </IfModule>

location / {
  rewrite ^/(.*)$ /site/redirect?u=$1;  
}

location /site {
  location /site/.*\.(?:xml|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|doc|xls|exe|ppt)$
    expires       max;
    access_log      off;
    log_not_found   off; 
 
  }
  location /site/private  {
     auth_basic            "Restricted";
     auth_basic_user_file  /home/MYDOMAIN/.htpasswds/public_html/site/private/passwd;
     <fastcgi block here>
  }
 
  location /site/wordpress  {
     ## Regular PHP processing.
     location ~ ^/site/wordpress/(?<script>.+\.php)(?<path_info>.*)$ {
            include fastcgi.conf;
            ## The fastcgi_params must be redefined from the ones
            ## given in fastcgi.conf. No longer standard names
            ## but arbitrary: named patterns in regex.
            fastcgi_param SCRIPT_FILENAME $document_root$script;
            fastcgi_param SCRIPT_NAME $script;
            fastcgi_param PATH_INFO $path_info;
            ## Passing the request upstream to the FastCGI
            ## listener.
            fastcgi_pass unix:/dev/shm/php5-fpm.MYDOMAIN.sock;;
      }
    }
  }
 
  location /site/redirect {
    <fastcgi block here>   
  }
  
  
  ## you need a try_files directive here but your "desired" config is
  ## quite confusing and I don't know what you need/want. Perhaps:
 
  try_files $uri $uri/ /site/wordpress; ## ???
}

For the fastcgi block use nested locations and named captures like the
above example, always prefixing the regex with the outer location
string, like in the /site/wordpress location.

--- appa



More information about the nginx mailing list