Web Analytics Made Easy -
StatCounter htaccess Help! (Make 1st level dir uppercase) - CodingForum

Announcement

Collapse
No announcement yet.

htaccess Help! (Make 1st level dir uppercase)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    Do you have access to the httpd.conf file or do you only have .htaccess file access?

    Comment


    • #3
      No, I don't have access to the httpd.conf file.

      Comment


      • #4
        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.)

        Comment


        • #5
          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.

          Comment


          • #6
            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?

            Comment


            • #7
              No, It didn't redirect.

              Comment


              • #8
                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; Apr 17, 2009, 07:15 PM.
                You can not say you know how to do something, until you can teach it to someone else.

                Comment


                • #9
                  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.

                  Comment


                  • #10
                    Thanks to everyone for the help.

                    The 404 redirect worked Great!!!

                    Comment

                    Working...
                    X