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

pk899 nginx-forum at nginx.us
Wed Jun 1 16:00:52 MSD 2011


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>



Here is what I have tried so far. I am almost there: 


    # SERVE CACHE STATIC FILES FIRST 
    location ~*
\.(xml|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|doc|xls|exe|ppt)$
 {
      try_files       $uri    =404;
      expires         max;
      access_log      off;
      log_not_found   off; 
    }

    # http://nginx.org/en/docs/http/converting_rewrite_rules.html
    location / {
        try_files 
              $uri 
              /site/redirect?u=$uri 
              @fromphp 
              =404;
    }

    # FOR ALL THE PHP HANDLING 
    location @fromphp {
      fastcgi_pass    unix:/dev/shm/php5-fpm.MYDOMAIN.sock;
      fastcgi_index   index;
      include         fastcgi_params;
      fastcgi_param   SCRIPT_FILENAME  
$document_root$fastcgi_script_name;
      fastcgi_param   PATH_TRANSLATED  
$document_root$fastcgi_script_name;
      fastcgi_param   SCRIPT_NAME       $fastcgi_script_name;
    }

    # FOR THE REST OF THE WEBSITE
    location /site/ {

        fastcgi_pass    unix:/dev/shm/php5-fpm.MYDOMAIN.sock;
        fastcgi_index   index;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME  
$document_root$fastcgi_script_name;
        fastcgi_param   PATH_TRANSLATED  
$document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME       $fastcgi_script_name;
  
        location ~ /site/private/ { 
          auth_basic            "Restricted";
          auth_basic_user_file 
/home/MYDOMAIN/.htpasswds/public_html/site/private/passwd;
        }
  
        location ~ \.php$ {
          #NOTE: Set "cgi.fix_pathinfo = 0;" in php.ini
          fastcgi_split_path_info   ^(.+\.php)(/.+)$;
          fastcgi_pass             
unix:/dev/shm/php5-fpm.MYDOMAIN.sock;
          fastcgi_intercept_errors  on;
          fastcgi_index             index.php;
          fastcgi_param             SCRIPT_FILENAME  
$document_root$fastcgi_script_name;
          fastcgi_param             PATH_TRANSLATED  
$document_root$fastcgi_script_name;
          fastcgi_param             SCRIPT_NAME      
$fastcgi_script_name;
        }

        location ~ /site/wordpress/ { 
          # Wordpress stuff 
          try_files $uri $uri/ /site/wordpress/index.php?q=$uri&$args;
          if (-f $request_filename)  {  expires 30d;  break; }
          if (-d $request_filename)  {  break; }
          if (!-e $request_filename) {  rewrite ^/site/wordpress/(.+)$
/site/wordpress/index.php?q=$1 last; }
        }
    
    }
    
    
    
    
Basically what is NOT working: 

1. The main directory which is supposed to redirect to
"/site/redirect?u=URI" 
2. The Wordpress stuff is downloading a text file (with the full PHP
code, which is scary) 


Thanks for any ideas or pointers!

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,202956,202956#msg-202956




More information about the nginx mailing list