homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

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)


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

2.Product images are present in the folder


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


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


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 https://images.example.com/static/index.php?/static/index.php
if a Product image is not found.

Where am I going wrong?

I dint get any reply on SO.

Thanks in advance,



 5:44 pm on Apr 16, 2014 (gmt 0)

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

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)


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.



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

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

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


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

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
will lead the browser to ask for images at
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
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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved