Creating a rewriteCond in .htaccess

Discuss general web development questions. Help others and get help from others.

Moderator: kfury77

Creating a rewriteCond in .htaccess

Postby chrisjlocke » Sat Jan 05, 2008 3:31 pm

I've a php script which creates thumbnail images, which is part of the CMS of my website. However, this also means I can't use it directly within vB or phpBB forums, as [img]script.php?image=xyz.jpg[/img] just doesn't work.

What I'd like to do is create a 'fake' file, eg, http://www.creapsoft.co.uk/thumbs/chris ... 8/file.jpg
which (via .htaccess) will be re-written as http://www.creapsoft.co.uk/protectedima ... &width=200

Could it be done? Could someone give me some pointers please?
User avatar
chrisjlocke
Top Contributor
 
Posts: 961
Joined: Mon Aug 01, 2005 4:12 pm
Location: Essex, UK

.htAccess Help

Postby s1tony » Wed Jan 09, 2008 9:25 pm

Chris,

Yes, something like this can be done. If you still would like some help with this, let me know.

Tony
s1tony
 
Posts: 15
Joined: Wed Jan 09, 2008 1:44 am

Postby chrisjlocke » Thu Jan 10, 2008 2:02 am

Hi Tony,

Yup, am still struggling with this. I downloaded RegexBuddy to try and get a grasp on regular expressions (great program!) but don't know enough yet to see how I can do what I want.
User avatar
chrisjlocke
Top Contributor
 
Posts: 961
Joined: Mon Aug 01, 2005 4:12 pm
Location: Essex, UK

Postby s1tony » Thu Jan 10, 2008 4:04 am

Cool - I will try to post something for you later tonight, including a few links I have found helpful concerning Apache Rewrites, Redirects and .htaccess.
s1tony
 
Posts: 15
Joined: Wed Jan 09, 2008 1:44 am

.htaccess Support

Postby s1tony » Thu Jan 10, 2008 8:12 am

Although I have never really found any outstanding documentation or tutorial for Apache URL Rewriting or other such .htaccess capabilities, the Apache documentation is decent, but not necessarily user-friendly:

Apache 1.3 (Apache 2.0 Guides also available, but almost the same functionality):
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
http://httpd.apache.org/docs/1.3/misc/rewriteguide.html

Other Useful References:
http://www.askapache.com/htaccess/mod_r ... ricks.html
http://www.htaccesselite.com/htaccess/
http://www.htaccesselite.com/htaccess/m ... vt101.html

The above references should get you going in the right direction, but I can also offer a little extra help.

What I'd like to do is create a 'fake' file, eg, http://www.creapsoft.co.uk/thumbs/chris ... 8/file.jpg
which (via .htaccess) will be re-written as http://www.creapsoft.co.uk/protectedima ... &width=200


Concerning your fake file setup, I use something similar in one of my projects and the cool thing is you do not even have to actually create the "fake" files.

I am sure this can be done several different ways, but here is one implementation to try:

- The basic layout of the .htaccess file:

Code: Select all
# Chris's .htaccess File

# FollowSymLinks is required to run the RewriteEngine, so enable the
# option
Options -Indexes
Options +FollowSymLinks

# Activate the RewriteEngine
RewriteEngine On

# You may need to define the RewriteBase, depending on how your file
# paths relates to your web paths - see documentation
# I do not do this in my setup
#RewriteBase /


# Rewrite URL for CMS Thumbs

# Use RewriteCond to verify the URL should be redirected to thumbs
# This may not be necessary
# RewriteCond

# Rewrite Rule
RewriteRule /thumbs/(.*) /(.*)/(.*)\.jpg$
protectedimage.php?image=$1/$3.jpg_$2&cachedimage=true&width=200 [L]

# This [L] flag means this is the last rule to process
# You may want to check out some of the other flags




- Place your .htaccess file in a directory above the directory referencing the "fake" files, such as

http://www.creapsoft.co.uk/thumbs/


- Test and good luck

I imagine the sample file is not exactly correct, but hopefully it will be able to lead you in the right direction.
s1tony
 
Posts: 15
Joined: Wed Jan 09, 2008 1:44 am

Postby chrisjlocke » Thu Jan 10, 2008 12:56 pm

Apart from an unnecessary space after the (.*), and a / needed before the 'protectedimage.php', I think thats going to work...
Can't thank you enough! Thats such a great help! I owe you a virtual beer or two! ;)
User avatar
chrisjlocke
Top Contributor
 
Posts: 961
Joined: Mon Aug 01, 2005 4:12 pm
Location: Essex, UK

Postby chrisjlocke » Thu Jan 10, 2008 1:31 pm

So far I've got this, but the server doesn't appear to be catching it.

RewriteRule /thumbs/(.*)/(.*)\.jpg_(.*)\.jpg$ /protectedimage.php?image=$1/$2.jpg_$3&cachedimage=true&width=200 [R=301,L]

I've had to tweak the URL slightly from my opening post. The URL I'm passing it is http://www.creapsoft.co.uk/thumbs/chris ... 012008.jpg and it should be re-mapping it to http://www.creapsoft.co.uk/protectedima ... &width=200

You've certainly led me in the right direction, and RegexBuddy confirms it *should* be working, but I don't know why it isn't. All I'm getting is a 404. Its not clear from the weblogs why though - just that the URL passed couldn't be found (rather than the rejigged URL being wrong).
User avatar
chrisjlocke
Top Contributor
 
Posts: 961
Joined: Mon Aug 01, 2005 4:12 pm
Location: Essex, UK

Postby chrisjlocke » Thu Jan 10, 2008 1:59 pm

Grr. Got it. URL doesn't start with a slash. Replace with a ^ and all is well! Argh!
User avatar
chrisjlocke
Top Contributor
 
Posts: 961
Joined: Mon Aug 01, 2005 4:12 pm
Location: Essex, UK

Postby s1tony » Sun Jan 20, 2008 3:19 am

You are certainly welcome - I am glad you were able to get things working.
s1tony
 
Posts: 15
Joined: Wed Jan 09, 2008 1:44 am

Postby chrisjlocke » Sun Jan 20, 2008 1:06 pm

It was such a help. Made me understand how it worked so I could use other 'variations' - eg, mysite.com/15/some_content.htm so that redirected to content.php?contentid=15, etc. Excellent!

If you were a girl, I'd kiss you! ;-)
User avatar
chrisjlocke
Top Contributor
 
Posts: 961
Joined: Mon Aug 01, 2005 4:12 pm
Location: Essex, UK


Return to Web Developer Talk

Who is online

Users browsing this forum: No registered users and 1 guest

cron