Welcome to WebmasterWorld Guest from 54.167.129.169

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect to index.php when static asset not found - Not working

Static assets on subdomain. When not found should redirect to index.php.

     
11:33 am on Apr 16, 2014 (gmt 0)

New User

joined:Apr 16, 2014
posts: 2
votes: 0


Problem:

1.example.com is using Codeigniter to serve Webpages with Products.

2.Product images are present in the folder

/static/prods/

3.All static content like images,css,js need to be served using a different sub domain like these

images.example.com/prods/productid.png

4.However if images.example.com/prods/productid.png
does not exist, it should serve Productimage generated dynamically by Codeigniter path

/index.php?/static/prods/prodid.png

where 'static' is a function on the 'home' controller .

5.Apache 2.2

6.Current htaccess

RewriteEngine On
RewriteBase /

#Render static files
RewriteCond %{HTTP_HOST} images.example.com [NC]
RewriteCond %{REQUEST_URI} !static/
RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /static/$1 [L]

RewriteCond %{HTTP_HOST} images.example.com [NC]
RewriteCond %{REQUEST_URI} !static/
RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI} !-f
RewriteRule ^(.*)$ https://images.example.com/static/$1 [L,R=303]

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]


#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]



7. The above htaccess incorrectly redirects to
redirects to [images.example.com...]
if a Product image is not found.


Where am I going wrong?

I dint get any reply on SO.

Thanks in advance,
Sid.
5:44 pm on Apr 16, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13264
votes: 363


Eeuw, what a mess.
<tangent>
This kind of thing explains why so many hosts charge by CPU usage rather than by space or bandwidth.
</tangent>

It looks like you've got a very common problem: a CMS with htaccess rules cobbled together from three or more sources, placed one after the other.

:: detour to refresh memory on what 303 is, as I never see it outside these forums ::

Oh, right, See Other.

The above htaccess incorrectly redirects to

How did you establish this? We're talking about non-page files, so the browser's address bar won't change. Are you using a tool such as Live Headers, or looking at raw logs?

In the quoted htaccess there is only one redirect. It's part of the pair:

"if the requested file would exist if you added '/static/' to the front of its URL, then serve content from there; otherwise
...
explicitly redirect to the identical place"
?

That doesn't make sense. And it also doesn't agree with the preceding prose, which leads us to expect something involving /index.php?/static/blahblah

Why are you redirecting to a location you've already established doesn't exist? For that matter, why do the -f check all over again when its results are guaranteed by the previous rule?

The final rewrite-- the all-encompassing CMS !-f !-d index.php rule-- would seem to overwrite all preceding rules. That includes all requests involving /static/ since they're not excluded at this stage. So it sounds as if the rule is doing exactly what it's been told to do, barring the extraneous /static/.

Incidentally: If the HTTP_HOST has already been explicitly identified as "images.example.com" then what's the DOCUMENT_ROOT business for?
3:53 am on Apr 19, 2014 (gmt 0)

New User

joined:Apr 16, 2014
posts: 2
votes: 0


Thanks.

Thats all my little dangerous knowledge could do. Tried to read as much as possible and asked in another forum for help but got no response.

Points 1 through 5 is what I need.

Most the htaccess was default for PHP Codeigniter. I just tried to add the '#Render Static files' part.

Struggled to avoid redirects but just couldn't figure out.

I am still not able to figure out the right htaccess rules.

Sid.
7:12 am on Apr 19, 2014 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13264
votes: 363


Yes, it's probably better to backtrack: first work out exactly what you want to do, and then hammer out code to do it.

2.Product images are present in the folder
/static/prods/

3.All static content like images,css,js need to be served using a different sub domain like these

images.example.com/prods/productid.png

Can you explain this? Ordinarily subdomains only have meaning for pages, where you might want the URL to say
goodstuff.example.com/blahblah
as an alternative to something like
example.com/goodstuff/blahblah

But what difference does it make in the case of non-page files? All that should matter is what directory they physically live in. By default, their URL will have the same root as the page that calls them, so
goodstuff.example.com/blahblah
will lead the browser to ask for images at
goodstuff.example.com/images/filename.jpg
and then it's your site's job to rewrite these requests, if necessary, to find the images wherever they actually live.

A form like
images.example.com/
has no physical existence. So no matter what you do, there will be rewriting involved. It seems as if you'd just be introducing an extra step.