Forum Moderators: phranque

Message Too Old, No Replies

Coexisting rewriting static to dynamic and dynamic access

         

Ayana

3:02 am on Mar 7, 2006 (gmt 0)

10+ Year Member



I want to convert my website to use static pages of the form [mysite.com...] (varying number of elements in the path) which are all processed by a single script at [mysite.com...]

However at the same time I'd like to keep links that go to specific existing pages working (ex. [mysite.com...]

Here's the .htaccess I wrote:

Options +FollowSymLinks +MultiViews 
AddType x-mapp-php5 .php

RewriteEngine on 
RewriteBase /
RewriteCond %{SCRIPT_FILENAME}!(\.[A-Za-z0-9]{2,4})$
RewriteRule ^(.*)$ /test/rw.php?p=$1 [L]

This seems to work well and treat URLs that end in a 2-4 character extension as regular pages and everything else as static-to-dynamic rewrites.

For example:
[mysite.com...] - gets sent to /test/rw.php?p=
[mysite.com...] - shows /index.php as desired
[mysite.com...] - gets sent to /test/rw.php?p=foo/bar/q/w/e/r

My problem is that:
[mysite.com...] gets sent to /test/rw.php?p=site/Contact.php instead of having that actual file shown.

Any ideas? Sorry if this is something very simple or obvious I'm just very very stumped why the rule works correctly for /index.php but not /site/Contact.php

jdMorgan

5:12 am on Mar 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've seen servers where SCRIPT_FILENAME was always blank or did not work as expected, so you might want to try

RewriteCond $1 !\.[a-z0-9]{2,4}$ [NC]
RewriteRule ^(.*)$ /test/rw.php?p=$1 [L]

instead. Note that $1 in the RewriteCond resolves to the same value as it does in the RewriteRule substitution -- It is the local URL-path matched by ^(.*)$ in the RewriteRule pattern. Using the [NC] flag (No Case) in the RewriteCond saves a couple of compare operations.

Jim

Ayana

9:41 pm on Mar 7, 2006 (gmt 0)

10+ Year Member



Thank you very much Jim! That fixed it wonderfully, and thanks for the optimization too!