Forum Moderators: phranque

Message Too Old, No Replies

Fetch html pages from different directory using htaccess

Fetch html pages from different directory using htaccess

         

Prabhanjan

9:25 am on Jul 15, 2008 (gmt 0)

10+ Year Member



Hi,
I have a cache system that generates html pages as cache and saves it to the exact request path so that next time a request comes no script get executed and the page is thrown to the client.

But the problem is I want it in a different directory
like
/htmlcache/

and would like .htaccess to do the check, if the page exists in the /htmlcache directory fetch it and display it.

I am not very good in .htaccess so can any on help me with this.
I even don't know whether it is possible or not :).

Thanks

jdMorgan

3:15 pm on Jul 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We don't provide a free code-writing service here (See Apache Forum Charter), but the following, in conjunction with the Apache mod_rewrite documentation, may help you get started:

File exists check:


RewriteCond %{REQUEST_FILENAME} -f

This only works when the URL points directly to the file.

It can be functionally replaced with


RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f

Using that form then allows you to insert additional filepath-info, for example:

RewriteCond %{DOCUMENT_ROOT}/subdirectory%{REQUEST_URI} -f

Using a pattern in the RewriteRule allows you to remove unwanted path info, and the result can be back-referenced by the RewriteCond. For example:

# Rewrite /<name>.html requests to /subdirectory/<name>.htm files if they exist
RewriteCond %{DOCUMENT_ROOT}/subdirectory/$1.htm -f
RewriteRule ^([^.]+)\.html$ /subdirectory/$1.htm [L]

Does that help?

Jim

Prabhanjan

8:00 am on Jul 16, 2008 (gmt 0)

10+ Year Member



Hi jdMorgan,
Thanks for the reply I was able to solve the problem.

Prabhanjan

g1smd

9:30 pm on Jul 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Post some of the steps that you took, or some excerpts of the main elements, for future use...

Prabhanjan

1:36 pm on Jul 17, 2008 (gmt 0)

10+ Year Member



Hi,
This is the code that I used and it worked. Please suggest am I doing it correct?

# Check if HTML page exists in HTMLCacheDir if exists display it.
RewriteCond %{REQUEST_URI} ([[:print:]])\.html
RewriteCond %{DOCUMENT_ROOT}/htmlcache%{REQUEST_URI} -f
RewriteRule (.*) /htmlcache%{REQUEST_URI} [L]

# Check for index.html pages in HTMLCacheDir incase of directory .
RewriteCond %{DOCUMENT_ROOT}/htmlcache%{REQUEST_URI} -d
RewriteCond %{DOCUMENT_ROOT}/htmlcache%{REQUEST_URI}index.html -f
RewriteRule (.*) /htmlcache%{REQUEST_URI}index.html [L]

Thanks for the help.
Prabhanjan Panigrahi

jdMorgan

2:16 pm on Jul 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the sub-pattern "[[:print:]]" supposed to mean in the following line?

RewriteCond %{REQUEST_URI} ([[:print:]])\.html

As it stands, it appears to mean "Any URL-path containing the single character "[", ":", "p", "r", "i", "n", or "t", followed by "]". Characters reserved for use in regular expressions must be escaped if you intend to match the literal character. For example, to match a period you must use "\." and to match square brackets you must use "\[" and "\]". This is true for all of the many characters that are used as tokens in regular expressions.

Also, be aware that you should test the URL-path in the RewriteRule itself when possible, because adding an extra RewriteCond to do it just slows down your code.

Jim

AjiNIMC

2:01 pm on Jul 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As far I recall [[:print:]] took care of all printable characters. I think I read it somewhere will coding those files. I need to recheck those pages.

jdMorgan

2:20 pm on Jul 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Be careful; While that may work in PERL or PHP, it probably won't work in mod_rewrite -- it certainly won't work in all versions of mod_rewrite...

Since what you are testing is a URL, only printable characters can/will appear anyway. So you could just as well use the pattern "^([^.]+)\.html$" there. However, if you use dots in directory names, it would need to be "^(.+)\.html$".

Jim

[edited by: jdMorgan at 2:22 pm (utc) on July 18, 2008]

Prabhanjan

7:30 am on Jul 19, 2008 (gmt 0)

10+ Year Member



I thought the same as AjiNIMC did. Thanks jdMorgan for clarifying thing. I have even reduce extra RewriteCond as you pointed out. here is the new code.

# Check if HTML page exists in HTMLCacheDir if exists display it.
RewriteCond %{DOCUMENT_ROOT}/htmlcache/$1.html -f
RewriteRule ^([^.]+)\.html$ /htmlcache/$1.html [L]

# Check for index.html pages in HTMLCacheDir incase of directory and display it.
RewriteCond %{DOCUMENT_ROOT}/htmlcache/$1 -d
#RewriteCond %{DOCUMENT_ROOT}/htmlcache/$1/index.html -f
RewriteRule ^(.*)$ /$1/index.html [L]

Thanks,
Prabhanjan Panigrahi

AjiNIMC

6:03 pm on Jul 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It worked well for me, so I never digged deep into it. I searched and saw the same code for some more places.

Its always good to go with something that works well everywhere.

Thanks Jim,

Regards,
Aji