Forum Moderators: phranque

Message Too Old, No Replies

Newbie: Redirecting

         

TOrpheus

6:26 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



Hi,

I want to have www.site.com/folderx/ to redirect to a processing page like, www.site.com/process.php?ID=folderx.

Basically, process.php needs to know the folderx name.

I only want it to redirect if that folder doesn't exist already. So if /user/ was a real folder on the system, it will process the script /user/index.php and NOT redirect to the process.php script at all :)

Hope you all can help!

Thanks!

sitz

10:12 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



There are other ways to do this (and this may need tweaking, depending on your actual goals). Note the following:

  • this assumes the request will be for www.site.com/folderx or www.site.com/folderx/; if, for instance, the request is for www.site.com/folderx/foo.html, this won't execute
  • if /folderx isn't a folder but *is* a file (or a symlink), this won't execute.


    #
    # turn on mod_rewrite
    RewriteEngine on
    #
    # verify that DOCROOT/REQUESTED_PATH is not a directory
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}!-d
    #
    # Rewrite; capture the non-slash characters after
    # the initial '/' in $1 and use that as the 'ID' to
    # pass to process.php
    RewriteRule ^/([^/]+)/?$ /process.php?ID=$1 [L]
  • TOrpheus

    10:26 pm on Mar 30, 2005 (gmt 0)

    10+ Year Member



    Hi Sitz,

    Thanks so much for the response.

    I'm going to try this right away :)

    However, as I'm a total Newbie, do I just put this in the .htaccess file on the web root?

    Do I need to restart Apache?

    Thanks!

    sitz

    12:21 pm on Mar 31, 2005 (gmt 0)

    10+ Year Member



    The preference, for performance reasons, it to add the mod_rewrite directives to httpd.conf. If you don't have write access to your httpd.conf (and if you're on a shared host that you don't personally run, I'd be shocked if you do), then the .htaccess file is where the directives need to go. Note the following:

    1) leading '/' characters should be removed from RewriteRule lines when placing the rules in .htaccess files; thus:


    RewriteRule ^/foo/bar /path/to/file [L]

    ...becomes...

    RewriteRule ^foo/bar /path/to/file [L]

    ...when placing the rule in a .htaccess file

    2) mod_rewrite needs to be both enabled on the server (the module needs to be loaded) and the FileInfo option to AllowOverride needs to be active for the directory containing your .htaccess file; If *either* of these not true, mod_rewrite will not work in .htaccess files.