Welcome! Log In Create A New Profile

Advanced

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

June 01, 2011 08:00AM
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!
Subject Author Posted

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

pk899 June 01, 2011 08:00AM

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

António P. P. Almeida June 01, 2011 09:08AM

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

pk899 June 01, 2011 09:23AM

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

pk899 June 01, 2011 11:30AM

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

pk899 June 02, 2011 06:11AM

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

pk899 June 03, 2011 02:38PM

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

pk899 June 03, 2011 02:40PM

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

António P. P. Almeida June 01, 2011 09:34AM

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

pk899 June 01, 2011 10:03AM

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

António P. P. Almeida June 01, 2011 10:26AM

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

pk899 June 01, 2011 10:32AM

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

António P. P. Almeida June 01, 2011 10:56AM

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

António P. P. Almeida June 01, 2011 11:46AM

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

António P. P. Almeida June 02, 2011 02:56PM

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

pk899 June 02, 2011 07:33PM

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

António P. P. Almeida June 02, 2011 08:42PM

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

pk899 June 02, 2011 09:22PM

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

António P. P. Almeida June 02, 2011 09:46PM

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

pk899 June 02, 2011 10:08PM

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

António P. P. Almeida June 02, 2011 10:22PM

Nginx Architecture in deep

Marcos Ortiz June 02, 2011 03:30PM

Re: Nginx Architecture in deep

Alexandr Gomoliako June 02, 2011 04:14PM

Re: Nginx Architecture in deep

Marcos Ortiz Valmaseda June 02, 2011 04:20PM

Re: Nginx Architecture in deep

Antoine Bonavita June 03, 2011 04:36AM

Re: Nginx Architecture in deep

pk899 June 04, 2011 12:26AM

Re: Nginx Architecture in deep

António P. P. Almeida June 04, 2011 12:10PM

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

António P. P. Almeida June 03, 2011 02:50PM

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

edogawaconan June 01, 2011 09:34AM

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

pk899 June 05, 2011 01:51AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 155
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready