Forum Moderators: phranque

Message Too Old, No Replies

Local Images Not Loading

         

WebsiteMgrs

8:59 pm on Dec 21, 2004 (gmt 0)

10+ Year Member



Being fairly new to mod_rewrite, I put together a .htaccess for an affiliate site that looks to see if a folder exists or not. If it doesn't exist, the URL is converted into a link from the main folder with the default affiliate's ID. Otherwise, if the folder exists, the URL is converted so the folder name is the affiliate ID.

That part is working, but the local images are not showing up. Only images linked from other sites are being displayed.

Here's the .htaccess I have so far.

RewriteEngine On
Options FollowSymLinks

# See if an image is requested and deliver as-is
RewriteCond %{REQUEST_URI} ^\.(gif¦jpg¦png)
RewriteRule ^.\.(gif¦jpg¦png)$ - [NC,L]

# See if the request file is a folder. If not, rewrite URL with IBA
RewriteCond %{REQUEST_FILENAME}!-d [OR]
RewriteCond %{REQUEST_FILENAME} =*.php* [OR]
RewriteCond %{REQUEST_FILENAME} =*.htm*
RewriteRule ^(.*) /\?IBA=$1 [QSA,L]

Here's a link to the test pages:
http://www.example.com/ Main page
http://www.example.com/jeff non-existing user
http://www.example.com/test existing user

Any ideas on how to rewrite that and get the local images to work are welcome.

Thanks!

Jim Hutchinson

[edited by: Brett_Tabke at 5:00 am (utc) on Dec. 22, 2004]
[edit reason] examplized. [/edit]

jdMorgan

5:46 am on Dec 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



WebsiteMgrs,

Welcome to WebmasterWorld!

Your image URI check will fail because you start-anchored it. The RewriteCond is redundant and not needed. The correct code would be:


# If an image is requested, bypass all subsequent rules
RewriteRule \.(gif¦jpg¦png)$ - [NC,L]

See our forum charter [webmasterworld.com] for a link to regular-expressions tutorial that may come in handy if that's not clear.

The directory rewrite code does not appear to do what you say you want to do, and so does not seem correct, but I can't tell from your description what you intended to accomplish with it. The tests for .php, .htm, and .html extensions are confusingly unexplained.

Note that posting on this forum changes pipe characters to broken pipe "¦" characters. You will have to edit them, and change them back to solid pipes if you cut and paste from this forum.

Jim

WebsiteMgrs

7:37 am on Dec 22, 2004 (gmt 0)

10+ Year Member



Hello Jim, thanks for the tip. The directory rewrite is meant to convert a link to a non-existent folder into a usable affiliate link. We allow affiliates to create their own usernames. In the past, we would then create a folder on the server and put an index.php file in it that would grab the folder name and redirect the browser to the main site with the username rewritten in the form of?IBA=username.

What I'm trying to do is move away from creating the folder since having several thousand of them in a single folder causes maintenance problems and can cause Apache to stop listing the files if there are too many.

The test for .htm* and .php files are there so we can continue processing those files. Ideally, if the file exists, we show it. Otherwise redirect the browser to the main page.

So what we have is: test for a folder, then the file. Images are to be left alone and delivered as-is. The code has been appearing to work except the images.

I hope that helps clear it up. If there a better way to write it, I'm open to learning a better way.

Thanks

Jim

WebsiteMgrs

3:03 pm on Dec 22, 2004 (gmt 0)

10+ Year Member



Jim,

Your code for the images works only if I don't call a specific file. Example, if I access /test/index.php the image doesn't show up. But if I go to /test/ it does. Must have something to do with the directory code? I tried many other ways of writing it but most of the variants resulted in a Page Not Found error, which is exactly what I'm trying to avoid.

# See if the request file is a folder. If not, rewrite URL with IBA
RewriteCond %{REQUEST_FILENAME}!-d [OR]
RewriteCond %{REQUEST_FILENAME} \.(php*¦htm*)
RewriteRule ^(.*) /\?IBA=$1 [QSA,L]

Any ideas?

Jim

jdMorgan

3:29 pm on Dec 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case, you can replace both rulesets with one:

# If request is not for html, htm, php, txt, gif, jpeg, jpg, or png files
RewriteCond %{REQUEST_URI} !\.(html?¦php¦txt¦gif¦jpe?g¦png)$
# and if the current query string does not include an aff id
RewriteCond %{QUERY_STRING} !IBA=
# and the requested resource does not exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# append aff query string to URL and any existing query string
RewriteRule (.*) /?IBA=$1 [QSA,L]

If your html, php, and image requests never include a query string, you can shorten this further:

# If request is not for html, htm, php, txt, gif, jpeg, jpg, or png files
RewriteCond %{REQUEST_URI} !\.(html?¦php¦txt¦gif¦jpe?g¦png)$
# and requested resource does not exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# append aff query string to URL
RewriteRule (.*) /?IBA=$1 [L]

The RewriteConds are ordered such that the most-efficient-to-check variables are placed first, saving the inefficient directory-exists check for last. The result is that in most cases, mod_rewrite will stop processing as early as possible, before having to do the directory-exists check, which involves querying the server filesystem.

The html? pattern matches either htm or html, and jpe?g matches either jpg or jpeg. I also added an exclusion for .txt files, in case you have or will have a robots.txt file. These may not be needed now, but are good future-proofing.

Reemember to replace all broken pipe "¦" characters with solid pipes if you cut and paste this code -- posting on this board replaces solid pipes with broken ones, and mod_rewrite requires solid pipes.

Jim

WebsiteMgrs

3:40 pm on Dec 22, 2004 (gmt 0)

10+ Year Member



Excellent! The code is cleaner and works. Now I'll continue studying regexp.

Thanks for your help. It took me a long time to find this forum and am very glad I did.

Jim