Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    rlc
    rlc is offline
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    htaccess Help! (Make 1st level dir uppercase)

    I'm new to htaccess.

    I need to change all first level directories to uppercase.

    Example:
    www.mydomain.com/mydir/index.php
    to
    www.mydomain.com/MYDIR/index.php

    If anyone can help, I would greatly appreciate it.

    Thanks,
    Rich

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Posts
    364
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Do you have access to the httpd.conf file or do you only have .htaccess file access?

  • #3
    rlc
    rlc is offline
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    No, I don't have access to the httpd.conf file.

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Posts
    364
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Then you will need a PHP script as well
    Code:
    Options +FollowSymLinks
    
    RewriteEngine On
    
    RewriteRule ^([^/]*[a-z][^/]*)(/.*)?$ /script.php?dir=$1&rest=$2 [QSA,L]
    PHP Code:
    <?php

    if(getenv('REDIRECT_STATUS') === false || !isset($_GET['dir'])
       || empty(
    $_GET['dir']) || !isset($_GET['rest']))
    {
      
    # The request was not processed by mod_rewrite.
      
    header('Location: http://' getenv('HTTP_HOST') . '/'true301);
    }
    else
    {
      
    $redirect strtoupper($_GET['dir']) . $_GET['rest'];
      
    $query_string getenv('QUERY_STRING');
      if(!empty(
    $query_string))
      {
        
    $redirect .= '?' $query_string;
      }
      
    header('Location: http://' getenv('HTTP_HOST') . '/' $redirecttrue301);
    }
    (Untested.)

  • #5
    rlc
    rlc is offline
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the info but I must be doing something wrong.

    I put the script in the root dir & added the above to the htaccess file but it doesn't uppercase the dir.

  • #6
    Regular Coder
    Join Date
    Oct 2004
    Posts
    364
    Thanks
    0
    Thanked 18 Times in 18 Posts
    If you put the following mod_rewrite in your .htaccess file
    Code:
    Options +FollowSymLinks
    
    RewriteEngine On
    
    RewriteRule Google http://www.google.com/? [NC,R,L]
    and go to http://example.com/Google, do you get redirected to Google?

  • #7
    rlc
    rlc is offline
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    No, It didn't redirect.

  • #8
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    If you haven't got htaccess with mod rewrite enabled which it seems you haven't Then the only thing i can think that you can do is have two folders in your document root one with capitals and one without. Which will suck if you have lots of PHP files to redirect from. But if its just one index file might not be so bad. Well will still suck but. And this is the index file you would put in the lower case folder.

    PHP Code:
    <?php
    $url 
    $_SERVER["PHP_SELF"];
    $url_folders explode ('/'ltrim($url"/"));
    $url_folders[0] = strtoupper($url_folders[0]);
    $url_folders "/"implode('/'$url_folders);
    $qs "";
    if(!empty(
    $_SERVER["QUERY_STRING"]))
        
    $qs "?"$_SERVER["QUERY_STRING"];
    $domain $_SERVER["SERVER_NAME"];
    header('Location: http://' $domain $url_folders .  $qs);
    ?>
    But again this sucks. It would work on linux server but not on a windows server. Because windows you can't have a lower and upper case version of the folders. Anyway if it was me I would contact your server host and ask about mod rewrite.
    Last edited by timgolding; 04-18-2009 at 01:15 AM.
    You can not say you know how to do something, until you can teach it to someone else.

  • #9
    Regular Coder
    Join Date
    Oct 2004
    Posts
    364
    Thanks
    0
    Thanked 18 Times in 18 Posts
    You can use a custom 404 handler instead.
    Code:
    ErrorDocument 404 /404handler.php
    and
    PHP Code:
    <?php

    $request_uri 
    getenv('REQUEST_URI');

    # Does the first directory contain a lower case letter?
    if(preg_match('~^/([^/]*[a-z][^/]*)(/(.*))?$'$request_uri$matches))
    {
      
    # Make the first directory upper case.
      
    $first_directory strtoupper($matches[1]);
      
    # Does the directory exist?
      
    if(is_dir(getenv('DOCUMENT_ROOT') . '/' $first_directory))
      {
        
    # Redirect to the upper case directory.
        
    header('Location: http://' getenv('HTTP_HOST') . '/' $first_directory '/' $matches[3], true301);
      }
    }

    ?>
    Normal 404 page here.

  • Users who have thanked schleppel for this post:

    rlc (04-19-2009)

  • #10
    rlc
    rlc is offline
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks to everyone for the help.

    The 404 redirect worked Great!!!


  •  

    Posting Permissions

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