SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)

    rewrite rule only if file not in folder, possible?

    Say I already have a list of other files and folders in the root directory of my site:
    Code:
    mysite/staticpage1.php 
    mysite/staticpage2.php
    mysite/folder1
    mysite/folder2
    ... etc
    And I now want to add a mod_rewrite rule that serves the content of requests like
    Code:
    mysite/dynamicpage1.php
    From
    Code:
    /pages/index.php
    and yet I do not want to interfere with the existing pages and folders, nor do I want to change file extensions.

    Is there a way of using mod_rewrite, or similar so that it (pseudo)

    if file is found, go there
    if folder is found, go there
    else get content of /pages/index.php?<URL>

    I would normally just create a folder and send the links to, and serve the content, from that folder - so have not really come across this challenge before.

    Is it possible to do this without invoking and handling a 404?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think this could work:
    Code:
    RewriteEngine on
    RewriteCond &#37;{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^(.+)$ index.php?$1 [L]
    The -f switch means "not files" and -d means "not dirs". So if those are both satisfied, the RewriteRule below is applied.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Thanks Raffles, I have tried that locally and it seems to work as expected.

    Nice one.

    (Fond memories of Cambridge, we lived ~there for ~8 yrs)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •