I have nginx rewrites, so would need to look at them to give good idea of what I’m trying.
location /trailer {
rewrite ^/?trailer/([-0-9a-zA-Z,-]+)/([-0-9a-zA-Z,-]+)?/?([0-9a-zA-Z,-]+)?$ /watchtrailer.php?fkey=$1&tkey=$2&tres=$3;
I would have placed the /? inside the next atom (and created a new atom for the one you already have - are you sure about the “,”? After all, it is a RESERVED character in a URI, i.e., you can’t use it the way you are!). That would leave $4 to replace $3 without confusing matters with an optional / embedded somewhere between your $2 and $3. Okay, I’m dizzy now, too, but (at least to me) it’s important to keep the optional parts collected, i.e., you don’t want a $3 without the / so making nginx decide where to break $2 and $3 is not a smart thing to do.
}
Now, this is the original link that works. I could leave the watchtrailer.php file to remain, allow that to give the variables supplied a test against the database. If row exists, it can then 301 redirect to the proper url (as rewritten below) or just send a 404. All done from the php file itself.
if (!-f $request_filename) {
rewrite ^/?([0-9a-zA-Z,-]+)?/?([0-9a-zA-Z,-]+)?/?([0-9a-zA-Z,-]+)?/?([0-9a-zA-Z,-]+)? /index.php?var1=$1&var2=$2&var3=$3&var4=$4;
Same comment but more complex because there are two optional /'s, one between $2 and $3 and another between $3 and $4! If I were nginx, I would give up the ghost and so something wild with the input and directives you’ve issued … just for fun!.
}
Included in the index file is the proper new-watchtrailer.php to handle the variables. So, with this, I’d have PHP handling everything in terms of doing 301 or 404. It’s okay this way, but sucks in regards that I will be checking the database twice per request to the old url (one from watchtrailer.php and then again from new-watchtrailer.php). Not an issue, save the old url will likely be hit about 1M times per day at the beginning.
I was hoping to go easier, and just do
location /trailer {
rewrite ^/?trailer/([-0-9a-zA-Z,-]+)/([-0-9a-zA-Z,-]+)?/?([0-9a-zA-Z,-]+)?$ /$1/$2 permanent;
}
That’s just your “delete trailer/” code (with the complication of the optional / between $1 and $2).
So just have nginx handle the 301 redirect right away, making new-watchtrailer.php handle the request and decide whether to 200 or 404 response at that point.
Let me know if that makes sense.
[indent]With all the /?'s in your code, no, it does not make sense to me (try a URI without the /'s and make a guess at what nginx will do and compare it with the output from your code).
Okay, I’m definitely biased: I prefer my method of matching ^([-a-z]+/[-a-z]+)$ and sending to handler.php?title=$1 OR, for your dual input: ^trailer/([-a-z]+/[-a-z]+)$ watchtrailer.php?movie=$1&trailer_name=$2.
For your if (!-f $request_filename) {
rewrite ^/?([0-9a-zA-Z,-]+)?/?([0-9a-zA-Z,-]+)?/?([0-9a-zA-Z,-]+)?/?([0-9a-zA-Z,-]+)? /index.php?var1=$1&var2=$2&var3=$3&var4=$4;
may I recommend you change the rewrite line to:
rewrite ^([0-9a-zA-Z-]+)(/([0-9a-zA-Z-]+)(/([0-9a-zA-Z-]+)(/([0-9a-zA-Z-]+)?)?)? /index.php?var1=$1&var2=$3&var3=$5&var4=$7;
to group your optional atoms (note that I’ve removed the “reserved” commas as they appear to be illegal as you’ve implied them to be used - see the referenced URI Generic Syntax from my first post and Find “Reserved”).[/indent]
Cheers!
Ryan