Welcome! Log In Create A New Profile

Advanced

Need help modifying $args variable

Posted by brockfanning 
Need help modifying $args variable
November 08, 2011 03:31PM
Hello, first time post. I have set up Nginx for a Drupal 7 site and am very happy with the performance so far. I just need some help in the rewrite/redirect department...

Problem: I am revamping a site with static .php files, and need to set up redirects from those old static URLs to "clean" versions. For example I need:
"about-us.php" to redirect to "about-us"

Problem 2: I only want the above to happen if the static file doesn't exist on the server. For example, if "about-us.php" actually exists on the server, I want to serve that file. (In other words, the client wants to launch the site without all the existing content ported to the new version of the site...)

Solution that I don't know how to do: I'd like to just modify the $args variable to remove the ".php" from it. Currently this snippet in my site config is doing the relevant work:

location / {
try_files $uri $uri/ /index.php?$args;
}

My thinking is that my problem would be solved if I had something like:

location / {
try_files $uri $uri/ /index.php?$args_without_dot_php;
}

However I don't know how to get the $args_without_dot_php.

Thanks for any help!
Re: Need help modifying $args variable
November 09, 2011 03:06AM
I don't unterstand the problem ...
Are there file names with extension in the "args" and you want to remove extentsion from within an argument ?

I'm not shure but try to match the "location" with something like "location ~ (.*).php" then you can use $1 "in try_files".

T.
Re: Need help modifying $args variable
November 09, 2011 12:32PM
Thank you, what you suggest should work. However it appears that paths to non-existent .php files do not make it to my try_files directive. I believe I was too quick to assume that I could fix the problem by modifying $args.

I think the answer I need is to figure out why non-existent .php file paths are not passing along to my try_files directive. I'll post my config below, but to illustrate the problem, here are some URLs:

http://50.56.85.187/about/class-visits
This page loads fine as is. It is Drupal page generated from the /about/class-visits URI.

http://50.56.85.187/about/class-visits.php
This page looks different because it loads an actual php file called class-visits.php in the /about directory.

http://50.56.85.187/about/class-visits.php2
I intentionally screwed up the extension to illustrate that this leads to the normal Drupal 404 page. In other words, this is making it to the fallback part of the try_files directive.

http://50.56.85.187/about/class-visits2.php
I intentionally screwed up the filename this time, to illustrate what happens when a non-existent .php file is requested. (just a general browser error page)

All the above URLs work as I expect except for the fourth one. I need to get that forth one working so that I can have non-existent legacy pages to redirect where I want them to.

Finally here is my config: (I'm a newb, and this is cobbled from the net and trial and error, so I'm sure there are plenty of inefficiencies, feel free to critique!)


server {
listen 80;
server_name 50.56.85.187;
root /sites/art-gallery/trunk;

if ($http_host != "50.56.85.187") {
rewrite ^ http://50.56.85.187$request_uri permanent;
}

index index.php index.html;

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

# Make sure files with the following extensions do not get loaded by nginx because nginx would display the source code, and these files can contain PASSWORDS!
location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ {
deny all;
}

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}

location / {
try_files $uri $uri/ /index.php?$args;
}

location ^~ /sites/default/files/styles/ {
index index.php index.html;

if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last;
break;
}
}

location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}

location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Re: Need help modifying $args variable
November 11, 2011 10:40AM
Well I appear to have figured out my own issue with some more trial and error. I added the try_files directive a second time, to the .php$ location block at the end. So now the last location block in my config is:

location ~ \.php$ {
try_files $uri $uri/ /index.php?$args;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Like said, this is now working as I wanted. If I did something evil though, please let me know! :)
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 132
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready