Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I just spent the last 10 hours of my life on this & am running in circles, so was hoping someone may be able to help me.

I want a specific URL to load like this:

http://example.com/f/2011/image.png?attribute=small

When a URL in a format such as this hits, I'd like to rewrite it to hit the server as:

http://example.com/generate.php?f=2011/image.png&attribute=small

Based on above, my question is two-fold:

  1. How can I rewrite the URL in htaccess to meet my requirements above?
  2. If the original URL didn't have the attribute query string parameter, how can I ensure attribute will be false/blank/etc when it hits the server via htaccess?
share|improve this question

3 Answers 3

RewriteEngine On
# If it's not an existing file...
RewriteCond %{REQUEST_FILENAME} !-f
# ...and it's not an existing directory...
RewriteCond %{REQUEST_FILENAME} !-d
# ...and it starts with "f/" then rewrite it to generate.php
RewriteRule ^f/(.*)$ generate.php?f=$1 [L,QSA]

The two RewriteCond can be omitted. More information:

RewriteRule

QSA flag

share|improve this answer
    
Thank you! Link references very helpful as well :) –  MrCS Nov 23 '11 at 15:56
RewriteEngine On

RewriteRule ^([a-z]+)/([0-9]+)/image.png?attribute=small$ generate.php?f=$2/image.png&attribute=small&%{QUERY_STRING} [L]
share|improve this answer

Here's one that have more chances to work:

RewriteEngine On
# If it's not an existing file...
RewriteCond %{REQUEST_FILENAME} !-f
# ...and it's not an existing directory...
RewriteCond %{REQUEST_FILENAME} !-d
# ...and it starts with "f/" then rewrite it to generate.php
RewriteRule ^([a-z]+)/([0-9]+)/image\.png$ /generate.php?f=$1 [QSA,L]

Please tell me if it works

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.