Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite for non-existent resources

         

fruitwerks

6:21 pm on Nov 20, 2010 (gmt 0)

10+ Year Member



I have a little problem, I have have a site that is one page, and I wanted to emulate friendly url handling somehow. I have done this for the human side of things by creating a custom 404 redirect, so any url takes you back to the index.php - the magic is that javascript reads the uri for ? params and displays the content accordingly.

Now since search engines don't know javascript, I need a solution for that. From my quick tests it appears you can't pass along the request body when using
ErrorDocument 404 /index.php
If I am wrong my attempts have failed.

So what I need is a rewrite rule that serves up any non-existent resource as index.php and preserves the ?'s and &'s.

I played with it, but didn't get very far. I'm very disabled in the matter of regex so any help would be appreciated.

Here is my current working .htacess

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.tld$
RewriteBase /
RewriteRule ^css/(.*\.css) /combine.php?type=css&files=$1
RewriteRule ^js/(.*\.js) /combine.php?type=javascript&files=$1
ErrorDocument 401 /index.php
ErrorDocument 404 /index.php


So the strategy here is to do [domain.tld...] - and there isn't anything static about it or I could do a few per-case rules...

The only actual page that serves anything is the index.php - the above link would really load index.php?act=foo

g1smd

6:33 pm on Nov 20, 2010 (gmt 0)

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



Javascript is the wrong solution for this.

The server has to send the 404 response in the HTTP header. You'll need the index.php file to examine the requested parameters and immediately send the HTTP 404 Not Found response along with the human-readable error messages.

I have done this for the human side of things by creating a custom 404 redirect, so any URL takes you back to the index.php

Displaying the 404 error message is never done via a redirect, and it is extremely bad form to show the same content as resides on the root index page as an ErrorDocument.

fruitwerks

7:23 pm on Nov 20, 2010 (gmt 0)

10+ Year Member



I understand about the 404, that was a temporary solution while I figure out the rewrite rules. The whole site is a single javascript app, but with the rewrite rules I can serve the text content of the target page for robots.

fruitwerks

10:57 pm on Nov 20, 2010 (gmt 0)

10+ Year Member



Well I upgraded to Apache 2.2.16 to take advantage on the FallbackResource directive, that did not work out so well.

"Request exceeded the limit of 10 subrequest nesting levels due to probable confguration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace."

I'm not about to start dealing with higher level configs...

Anyway I think rewrite will be the best solution.

jdMorgan

3:47 am on Nov 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest:

RewriteEngine on
RewriteBase /
#
RewriteRule ^css/(.+\.css)$ /combine.php?type=css&files=$1 [L]
#
RewriteRule ^js/(.+\.js)$ /combine.php?type=javascript&files=$1 [L]
#
RewriteCond $1 !^(index|combine)\.php$
RewriteRule ^(.*)$ /index.php?files=$1 [L]

I have no idea what the original RewriteCond was for, but it couldn't work, so I removed it.

As g1smd stated, your "index.php" script must now validate the "files" parameter, and if it has no unique content for that specific URL-path, then the script itself must produce a correct 404-Not Found HTTP status response; It would be a mistake of rather significant proportions to omit this function, as your site would then have an infinite URL-space, and search engines would arbitrarily limit their crawling as an act of self-protection... Do not "put this off til later."

Jim