This is a discussion on mod_rewrite On-the-fly Content-Regeneration not working within the Apache Web Server forums, part of the Web Server and Related Forums category; Hello, After reading the URL rewriting guide and several other documents, I still haven't figured this out. Basically this ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello,
After reading the URL rewriting guide and several other documents, I still haven't figured this out. Basically this what I am trying to approach: - User requests domain.com/page.html - mod_rewrite checks if requested page exists in domain.com/static/page.html - if page does exist, it's served - if it does not exist, mod_rewrite redirects to domain.com/page.php - domain.com/page.php generates domain.com/static/page.html So, assuming the original URL is domain.com/page.html here are my last two (failed) attempts: # Attempt 1: # enable the rewrite module RewriteEngine on # use static html file if exists (from static/) RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes] RewriteCond static%{REQUEST_FILENAME}.html -f RewriteRule ^(.*)$ static$1.html [S=1] RewriteCond %{ENV:WasHTML} ^yes$ RewriteRule ^(.*)$ $1.php # Attempt 2: # enable the rewrite module RewriteEngine on # rewrite to php page if request does not exist in domain.com/static RewriteCond static%{REQUEST_FILENAME} !-s RewriteRule ^(.*)\.html$ $1.php [L] What am I doing wrong? I know this can work if I keep the HTML and PHP file in the same directory, but shouldn't it be possible to keep the static file in another directory as well? The reason why is that I only want the static directory to have writing permissions, others not. Thanks in advance, DrTebi |
|
|||
|
DrTebi wrote:
> Hello, > After reading the URL rewriting guide and several other documents, I > still haven't figured this out. > Basically this what I am trying to approach: > - User requests domain.com/page.html > - mod_rewrite checks if requested page exists in > domain.com/static/page.html > - if page does exist, it's served > - if it does not exist, mod_rewrite redirects to domain.com/page.php > - domain.com/page.php generates domain.com/static/page.html > > So, assuming the original URL is domain.com/page.html > here are my last two (failed) attempts: > # Attempt 1: > # enable the rewrite module > RewriteEngine on > > # use static html file if exists (from static/) > RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes] > > RewriteCond static%{REQUEST_FILENAME}.html -f > RewriteRule ^(.*)$ static$1.html [S=1] Did you try the following at all? RewriteCond static/%{REQUEST_FILENAME}.html -f RewriteRule ^(.*)$ static/$1.html [S=1] -- Justin Koivisto - spam@koivi.com http://www.koivi.com |
|
|||
|
On Thu, 30 Sep 2004 13:13:50 +0000, Justin Koivisto wrote:
> DrTebi wrote: > >> Hello, >> After reading the URL rewriting guide and several other documents, I >> still haven't figured this out. >> Basically this what I am trying to approach: >> - User requests domain.com/page.html >> - mod_rewrite checks if requested page exists in >> domain.com/static/page.html >> - if page does exist, it's served >> - if it does not exist, mod_rewrite redirects to domain.com/page.php >> - domain.com/page.php generates domain.com/static/page.html >> >> So, assuming the original URL is domain.com/page.html >> here are my last two (failed) attempts: >> # Attempt 1: >> # enable the rewrite module >> RewriteEngine on >> >> # use static html file if exists (from static/) >> RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes] >> >> RewriteCond static%{REQUEST_FILENAME}.html -f >> RewriteRule ^(.*)$ static$1.html [S=1] > > Did you try the following at all? > > RewriteCond static/%{REQUEST_FILENAME}.html -f > RewriteRule ^(.*)$ static/$1.html [S=1] I just tried that, but it did not work either unfortunately. What exactly is in "%{REQUEST_FILENAME}" if, let's say the URL is http://domain.com/page.html shouldn't that be "page.html"? Or "/page.html"? still confused :( |
|
|||
|
DrTebi wrote:
> On Thu, 30 Sep 2004 13:13:50 +0000, Justin Koivisto wrote: > > >>DrTebi wrote: >> >> >>>Hello, >>>After reading the URL rewriting guide and several other documents, I >>>still haven't figured this out. >>>Basically this what I am trying to approach: >>>- User requests domain.com/page.html >>>- mod_rewrite checks if requested page exists in >>> domain.com/static/page.html >>>- if page does exist, it's served >>>- if it does not exist, mod_rewrite redirects to domain.com/page.php >>>- domain.com/page.php generates domain.com/static/page.html >>> >>>So, assuming the original URL is domain.com/page.html >>>here are my last two (failed) attempts: >>># Attempt 1: >>># enable the rewrite module >>>RewriteEngine on >>> >>># use static html file if exists (from static/) >>>RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes] >>> >>>RewriteCond static%{REQUEST_FILENAME}.html -f >>>RewriteRule ^(.*)$ static$1.html [S=1] >> >>Did you try the following at all? >> >>RewriteCond static/%{REQUEST_FILENAME}.html -f >>RewriteRule ^(.*)$ static/$1.html [S=1] > > I just tried that, but it did not work either unfortunately. > What exactly is in "%{REQUEST_FILENAME}" if, let's say the URL is > http://domain.com/page.html > shouldn't that be "page.html"? Or "/page.html"? > > still confused :( Umm.. neither... more like: "/home/user/www/page.html", so change to this: RewriteCond static%{REQUEST_URI} -f RewriteRule ^(.*)$ static/$1.html [S=1] -- Justin Koivisto - spam@koivi.com http://www.koivi.com |
|
|||
|
On Thu, 30 Sep 2004 14:50:09 +0000, Justin Koivisto wrote:
> DrTebi wrote: > >> On Thu, 30 Sep 2004 13:13:50 +0000, Justin Koivisto wrote: >> >> >>>DrTebi wrote: >>> >>> >>>>Hello, >>>>After reading the URL rewriting guide and several other documents, I >>>>still haven't figured this out. >>>>Basically this what I am trying to approach: >>>>- User requests domain.com/page.html >>>>- mod_rewrite checks if requested page exists in >>>> domain.com/static/page.html >>>>- if page does exist, it's served >>>>- if it does not exist, mod_rewrite redirects to domain.com/page.php >>>>- domain.com/page.php generates domain.com/static/page.html >>>> >>>>So, assuming the original URL is domain.com/page.html >>>>here are my last two (failed) attempts: >>>># Attempt 1: >>>># enable the rewrite module >>>>RewriteEngine on >>>> >>>># use static html file if exists (from static/) >>>>RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes] >>>> >>>>RewriteCond static%{REQUEST_FILENAME}.html -f >>>>RewriteRule ^(.*)$ static$1.html [S=1] >>> >>>Did you try the following at all? >>> >>>RewriteCond static/%{REQUEST_FILENAME}.html -f >>>RewriteRule ^(.*)$ static/$1.html [S=1] >> >> I just tried that, but it did not work either unfortunately. >> What exactly is in "%{REQUEST_FILENAME}" if, let's say the URL is >> http://domain.com/page.html >> shouldn't that be "page.html"? Or "/page.html"? >> >> still confused :( > > Umm.. neither... more like: "/home/user/www/page.html", so change to this: > > RewriteCond static%{REQUEST_URI} -f > RewriteRule ^(.*)$ static/$1.html [S=1] Still not :( I tried this, which seems logical to me (note the comments): # remove .html extension, chain with next rule (/page.html -> /page) RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes] # rewrite to /static/page.html if exists, skip next rule (/page -> /static/page.html) RewriteCond /static$1.html -f RewriteRule ^(.*)$ /static/$1.html [S=1] # if previous failed, but first rule matched, rewrite to .php extension (/page -> /page.php) RewriteCond %{ENV:WasHTML} ^yes$ RewriteRule ^(.*)$ $1.php Please tell me two things: 1) what's the original before I do a rewrite, let's say of http://domain.com/page.html I assumed /page.html 2) Once a rewrite took place, like stripping the .html from the request, is the next rule going to work on /page or still on /page.html? thanks :) |
|
|||
|
DrTebi wrote:
> On Thu, 30 Sep 2004 14:50:09 +0000, Justin Koivisto wrote: >>DrTebi wrote: >>>On Thu, 30 Sep 2004 13:13:50 +0000, Justin Koivisto wrote: >>>>DrTebi wrote: >>>> >>>>>Basically this what I am trying to approach: >>>>>- User requests domain.com/page.html >>>>>- mod_rewrite checks if requested page exists in >>>>> domain.com/static/page.html >>>>>- if page does exist, it's served >>>>>- if it does not exist, mod_rewrite redirects to domain.com/page.php >>>>>- domain.com/page.php generates domain.com/static/page.html <snip bad info ;) > > Please tell me two things: > 1) what's the original before I do a rewrite, > let's say of http://domain.com/page.html > I assumed /page.html %{REQUEST_URI} = /page.html > 2) Once a rewrite took place, like stripping the .html > from the request, is the next rule going to work on > /page or still on /page.html? When you perform a RewriteRule, you are then working with the result of the rule, not the original request anymore... Here's how I think it should work for what you want to accomplish (turn on rewrite log file and check if you want to see): User sends GET request to: http://domain.com/page.html %{REQUEST_URI} = /page.html RewriteCond static%{REQUEST_URI} -f If static/page.html is a file, do the next rule RewriteRule ^(.*)\.html$ static/$1.html [L] Rewrite /page.html to /static/page.html, end processing (L flag) RewriteRule ^(.*)\.html$ $1.php [L] Rewrite /page/html tp /page.php, end processing This rule is only reached if no L flag was encountered above. -- Justin Koivisto - spam@koivi.com http://www.koivi.com |
|
|||
|
On Thu, 30 Sep 2004 15:55:12 +0000, Justin Koivisto wrote:
> DrTebi wrote: >> On Thu, 30 Sep 2004 14:50:09 +0000, Justin Koivisto wrote: >>>DrTebi wrote: >>>>On Thu, 30 Sep 2004 13:13:50 +0000, Justin Koivisto wrote: >>>>>DrTebi wrote: >>>>> >>>>>>Basically this what I am trying to approach: >>>>>>- User requests domain.com/page.html >>>>>>- mod_rewrite checks if requested page exists in >>>>>> domain.com/static/page.html >>>>>>- if page does exist, it's served >>>>>>- if it does not exist, mod_rewrite redirects to domain.com/page.php >>>>>>- domain.com/page.php generates domain.com/static/page.html > > <snip bad info ;) > > >> Please tell me two things: >> 1) what's the original before I do a rewrite, >> let's say of http://domain.com/page.html >> I assumed /page.html > > %{REQUEST_URI} = /page.html > >> 2) Once a rewrite took place, like stripping the .html >> from the request, is the next rule going to work on >> /page or still on /page.html? > > When you perform a RewriteRule, you are then working with the result of > the rule, not the original request anymore... > > > Here's how I think it should work for what you want to accomplish (turn > on rewrite log file and check if you want to see): > > User sends GET request to: http://domain.com/page.html > %{REQUEST_URI} = /page.html > > RewriteCond static%{REQUEST_URI} -f > If static/page.html is a file, do the next rule > > RewriteRule ^(.*)\.html$ static/$1.html [L] > Rewrite /page.html to /static/page.html, end processing (L flag) > > RewriteRule ^(.*)\.html$ $1.php [L] > Rewrite /page/html tp /page.php, end processing > This rule is only reached if no L flag was encountered above. Thank you so much for helping... your idea is very logical, but still does not work. The problem is simply looping: /page.html requested /static/page.html exists, redirect to /static/page.html /static.page.html requested /static/static/page.html does not exist /page.php Can you see that? I am pretty sure that's it. I have hacked together now ansomething that works, by having the user request .htm pages, and look for.html pages in the static directory: # Dynamic Caching 6 (this finally works ;)) RewriteRule ^(.*)\.htm$ $1 [C,E=HTM:yes] RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI}l -f RewriteRule ^(.*)$ static/$1.html [S=1] RewriteCond %{ENV:HTM} ^yes$ RewriteRule ^(.*)$ $1.php This would go like this: page.htm requested page.htm rewritten to /page domain.com/static/page.html condition matches static/page.html requested end ;) And would the page not exist: page.htm requested page.htm rewritten to /page domain.com/static/page.html condition does not match page.php requested end Puhh! That was a hard one. It's a bit ugly now, but I will get used to it ;) Thanks for all your help! DrTebi |
|
|||
|
Now,
here is my final solution, it looks nicer and is much shorter, however I have a final question... read on: Idea: user requests domain.com/page.php mod_rewrite checks if domain.com/static/page.php.static exists if yes: Apache serves domain.com/static/page.php.static if no: the original request, domain.com/page.php will be served domain.com/page.php generates domain.com/static/page.php.static RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI}.static -f RewriteRule ^(.*)$ static/$1.static [L] The question I have now is how Apache handles the PHP Interpreter. Will it initialize it _before_ the mod_rewrite rules, since it was a .php request, or will it wait for mod_rewrite to rewrite the URL, and _not_ initialize the PHP interpreter? Since the whole idea is to avoid this initialization, I hope the latter is true. I found that PHP pages served with only a couple simple MySQL statements can already be up to 20 times slower than serving the static page instead. DrTebi |
|
|||
|
DrTebi wrote:
> On Thu, 30 Sep 2004 15:55:12 +0000, Justin Koivisto wrote: > > >>DrTebi wrote: >> >>>On Thu, 30 Sep 2004 14:50:09 +0000, Justin Koivisto wrote: >>> >>>>DrTebi wrote: >>>> >>>>>On Thu, 30 Sep 2004 13:13:50 +0000, Justin Koivisto wrote: >>>>> >>>>>>DrTebi wrote: >>>>>> >>>>>> >>>>>>>Basically this what I am trying to approach: >>>>>>>- User requests domain.com/page.html >>>>>>>- mod_rewrite checks if requested page exists in >>>>>>>domain.com/static/page.html >>>>>>>- if page does exist, it's served >>>>>>>- if it does not exist, mod_rewrite redirects to domain.com/page.php >>>>>>>- domain.com/page.php generates domain.com/static/page.html >> >><snip bad info ;) > >> >>>Please tell me two things: >>>1) what's the original before I do a rewrite, >>>let's say of http://domain.com/page.html >>>I assumed /page.html >> >>%{REQUEST_URI} = /page.html >> >> >>>2) Once a rewrite took place, like stripping the .html >>>from the request, is the next rule going to work on >>>/page or still on /page.html? >> >>When you perform a RewriteRule, you are then working with the result of >>the rule, not the original request anymore... >> >> >>Here's how I think it should work for what you want to accomplish (turn >>on rewrite log file and check if you want to see): >> >>User sends GET request to: http://domain.com/page.html >> %{REQUEST_URI} = /page.html >> >>RewriteCond static%{REQUEST_URI} -f >> If static/page.html is a file, do the next rule >> >>RewriteRule ^(.*)\.html$ static/$1.html [L] >> Rewrite /page.html to /static/page.html, end processing (L flag) >> >>RewriteRule ^(.*)\.html$ $1.php [L] >> Rewrite /page/html tp /page.php, end processing >> This rule is only reached if no L flag was encountered above. > > > Thank you so much for helping... your idea is very logical, but still does > not work. The problem is simply looping: > /page.html requested > /static/page.html exists, redirect to /static/page.html > /static.page.html requested > /static/static/page.html does not exist > /page.php > > Can you see that? I am pretty sure that's it. I have hacked together now > ansomething that works, by having the user request .htm pages, and look > for.html pages in the static directory: > # Dynamic Caching 6 (this finally works ;)) > RewriteRule ^(.*)\.htm$ $1 [C,E=HTM:yes] > RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI}l -f > RewriteRule ^(.*)$ static/$1.html [S=1] > RewriteCond %{ENV:HTM} ^yes$ > RewriteRule ^(.*)$ $1.php > > This would go like this: > page.htm requested > page.htm rewritten to /page > domain.com/static/page.html condition matches > static/page.html requested > end ;) > > And would the page not exist: > page.htm requested > page.htm rewritten to /page > domain.com/static/page.html condition does not match > page.php requested > end > > Puhh! That was a hard one. It's a bit ugly now, but I will get used to it ;) > Thanks for all your help! > > DrTebi > Ahh... simple logic error: RewriteCond %{REQUEST_URI} !^/static RewriteCond static%{REQUEST_URI} -f RewriteRule ^(.*)\.html$ static/$1.html [L] RewriteRule ^(.*)\.html$ $1.php [L] That should actually do it. No need to tell it to skip or set environment variables. ;) -- Justin Koivisto - spam@koivi.com http://www.koivi.com |
|
|||
|
DrTebi wrote:
> Now, > here is my final solution, it looks nicer and is much shorter, however I > have a final question... read on: > > Idea: > user requests domain.com/page.php > mod_rewrite checks if domain.com/static/page.php.static exists > if yes: Apache serves domain.com/static/page.php.static > if no: the original request, domain.com/page.php will be served > domain.com/page.php generates domain.com/static/page.php.static > > RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI}.static -f > RewriteRule ^(.*)$ static/$1.static [L] > > The question I have now is how Apache handles the PHP Interpreter. Will it > initialize it _before_ the mod_rewrite rules, since it was a .php request, > or will it wait for mod_rewrite to rewrite the URL, and _not_ initialize > the PHP interpreter? > > Since the whole idea is to avoid this initialization, I hope the latter is > true. I found that PHP pages served with only a couple simple MySQL > statements can already be up to 20 times slower than serving the static > page instead. All Apache stuff will finish before the page is attempted to load. PHP won't fire until the page load attempt. -- Justin Koivisto - spam@koivi.com http://www.koivi.com |