Forum Moderators: phranque

Message Too Old, No Replies

Need some quick .htaccess help!

         

Tryst

7:12 am on Dec 26, 2010 (gmt 0)

10+ Year Member



Hello, I'm a complete newbie with the .htaccess file and I'm having some trouble.

Firstly, this is the sort of URL I'm dealing with:
http://site.com/file.php?char=name&back=background


The full link will generate a png image with a character's name and a background. So, char=Character&back=trees would generate an image for a character named 'Character' using a background named 'trees'.

I've currently modified my .htaccess file to enable the image to be generated via:
http://site.com/Character.png


...however I'm having trouble adding the background part in.

What I want is something like this:
http://site.com/trees/Character.png


RewriteRule ^([a-zA-Z0-9_-]+)\.png$ file.php?char=$1\&background=$2
RewriteRule ^([a-zA-Z0-9_-]+)/\.png$ file.php?char=$1\&background=$2


I'd really appreciate it if someone could solve this for me! :)

g1smd

8:25 am on Dec 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The pattern contains one set of parentheses and does not allow slash, so all of the requested URL ends up in $1. Split the pattern into two with a slash and add another set of parentheses.

No escaping is required in the target filepath, and you must add the [L] flag to the RewriteRule.

Tryst

12:36 pm on Dec 26, 2010 (gmt 0)

10+ Year Member



Thanks for the reply, but as I said before I'm a complete newbie at this. I've added [L] to the end of each rule, but couldn't work out how to add the second parentheses.

g1smd

5:08 pm on Dec 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Like this?

^([A-Z0-9_\-]+)/([A-Z0-9_\-]+)\.png$


Use A-Z with the [NC] flag for faster operation (instead of A-Za-z here).

Tryst

5:16 pm on Dec 26, 2010 (gmt 0)

10+ Year Member



Works like a charm! Thank you very much! :)