Hi all...

I am making a url redirection service and I thought that the best way (to minimise file sizes) was to let the user choose a subdirectory of my site.

At the moment the script (which dosen't work) gives the user a random number which is added onto the url in the querystring (ie: domain.com/?r=03885). This, I think is harder to remember than my thought.

I want to be able to let users pick their ending, and then add it onto the end like a directory. So if I were to go to my site and sign up then I would use the username PoorDoggie and I would get "domain.com/poordoggie".

The way I was thinking about doing this is this: when someone signs up, it automatically creates a directory called, well their username, and in that directory the index.php page basically has this code:

<? php include('##user's website##'); ?>

What I would like is subdomains for my users, but I don't know how to us php to make subdomains automatically!

What I need help with is either, a way of autmatically creating directories and php files or a way to redirect any directory typed in to a special script.

The latter works like this: you type in "domain.com/poordoggie" (for instance) and it directs it to a script (without changing the url... domain cloaking I think its called) that takes the directory name and looks it up in a mysql database. The mysql bit is easy, its the directing to a script and cloaking, then getting the username from the URL thats the bit I can't do!

Please help me <-- look at these eyes!

Thanks a lot - in advance... infact thanks for just reading it all!

Tom

    well, you could prob use php to create subdomains. im not exactly sure how, but you will prob need to use php to write/modify some file that apache will use to see if the subdomains exists, and if so, where to map the subdomains document root to.

    i would search the apache manuals for subdomains to find out more on that. it may be possible all you need to do is have an .htaccess file to add the subdomain info to, which could easily be modified/written with php.

    you can make directories in php by using
    mkdir()

    you can write an index.php file or whatever file you want by using
    fopen()
    fwrite()
    fclose()
    theres also
    copy()

    if you wanted to make any request execute a specific php script, you could easily do that with apaches mod_rewite. theres tons of info on mod_rewrite on the web, and in apaches manual. using mod_rewrite, you could eliminate having to make a directory and php file for every user who signs up.

    i think mod_rewrite is prob what you would want to use, and it can be confusing to begginners, so ill give you some code that should start you in the right direction.

    ps- it may be possible to use mod_rewite for subdomains too, but ive never looked into it.

    this would be your .htaccess file in your doc root

    RewriteEngine On
    
    # make sure that what they requested is not an actual existing directory
    RewriteCond %{REQUEST_URI} !-d
    
    # make sure that what they requested is not an actual existing file
    RewriteCond %{REQUEST_URI} !-f
    
    # if the url they requested consists entirely of characters that are NOT forward slash, 
    # but may optionally end in a forward slash, rewite this request to script.php
    RewriteRule ^[^/]+/?$ script.php
    
    

    then in script.php, use the variable
    $_SERVER['REQUEST_URI'] to see what url they entered. just parse the info you want out of it, and then use that info to do whatever you gotta do.

    heres some examples of how the rewrite code would behave

    http://example.org/poordoggie <-- will get rewritten to script.php
    http://example.org/poordoggie/ <-- will get rewritten to script.php, trailing slash is optional
    http://example.org/index.php <-- if index.php is a real file on your server, it will NOT be rewritten. if the file doesnt exist, it WILL be rewritten
    http://example.org/images <-- if images is a real directory on your server, it will NOT be rewritten. if the directory doesnt exist, it WILL be rewritten
    http://example.org/somedir/foo.html <-- will NOT be rewritten. contains forward slash and the slash isnt the last character.
    http://example.org/somedir/poordoggie <-- will NOT be rewritten. contains forward slash and the slash isnt the last character.
    http://example.org/somedir/poordoggie/ <-- will NOT be rewritten. contains forward slash and the slash isnt the last character.

      Wow... thanks 🙂

      Let me get this straight - any url that is written as a directory in the root folder (ie: www.domain.com/folder OR www.domain.com/folder/) will get rewritten to "script.php" but anything in other folders, or folders/files that already exist will not be written to!

      Wow... thats wonderful and just what I need!

      Thanks a lot
      Tom

      🙂🙂🙂

        right... tried it out, but unfortunately the index.php is also redirected (well, if you goto www.website.com it redirects to script.php)

        is there any way out of this?

        Thanks a lot 🙂

          Infact, the "file exists" thing dosen't work as if I goto mywebsite.com or mywebsite.com/index.php or even mywebsite.com/images... they all redirect to script.php

            the subdomain thing cant be done with php, as far as i know you still need dns records of your subdomains.
            then if you manage to point .yourdomain.com to -> ip address of yourdomain.com THEN you can use the virtualdomain thing in apache (the alias line to point .domain to the same rules .. ?)..

            but i still think you need some special stuff for the dns records..
            i know there is no "i think" in this business so investigate until you get your answer

            AAANNDDD THEEEEEEENNN:

            for the /directory thing i've previously thought of this trick:

            1: first create directories for your users like /poordoggie or whatever
            2: disable all directory listings.
            3: create a single php error document that instead of showing the "no directory listing allowed" message - redirects the user to appropriate site BY:
            first find out the username by parsing the URL,
            then lookup the site in db, and then redirect using header(location: ) or a meta tag.

            oor something in that direction..

              ozzythaman - I was thinking about that. I though that it would be better if I could make a directory for each new user. It would be easier

              new users would be done like this:

              if(file_exists(##chosen username##/index.php)){
              echo "sorry username taken"
              }
              else{
              mkdir(##username##);
              /*
              etcetera... to make index.php which is just a
              page that uses
              "include('##users website address##')"
              to display the users website.
              */
              }
              

              then when people goto mysite.com/username then there is a file that does the redirecting. Especially as I can't for the life of me do the mysql. Hosts are funny, they don't allow some mysql functions for some reason, and then the other hosts won't allow the ones you have used when you change!

              I though about that, but I figured that space would run out very quickly, and also I couldn't have an "admin" pannel.

              Well I have a way to get round that!

              The index.php page in the directory needs to just have this code:

              include('mysite.com/script.php?url='.##user's url##);

              This won't take up too much space will it?

              Then the admin panel can just include all the index files in all the directories (just like an image gallery, I know the concept!).

              Will this work? Will it dramatically increase space or dramatically increase traffic useage?

              Thanks
              Tom

                ummm...

                you do NOT want to use include in your index.php files!

                THAT would be a major security mistake, big as a mountain

                you can not use include (and its probably disabled in the first place, because of the security concerns, if you're paying for hosting)

                when you do a include( remote file ), php DOWNLOADS the remote file and EXECUTES it (runs it), this means that all code that the users have put in their php pages - are executed - so they have total control of your system, they could delete all of your files for instance..

                you could instead just echo the contents of the remote page by using file system functions, but:

                you were talking about redirecting, any opening of the users remote file and then showing it on your pages would not be redirecting of the page viewers, but redirecting (TUNNELING) of the other site, you would use your self as a PROXY so to speak.
                is this really what you want to do ??

                or do you instead want to SEND the viewer to the other site (this is what all the redirecting companies do on the net)

                in that case you would put this line of code in your php script:
                header ( "location: http://blabla.com");

                and this i dont get:

                The index.php page in the directory needs to just have this code: PHP: include('mysite.com/script.php?url='.##user's url##);

                what are you trying to do, exactly ?

                  sorry if I have not explained myself properly

                  I want to create a URL redirection service.

                  I WANT to tunnel users. Apparantly there is big money in doing this as I can then place adverts on the page and make money (people I know have done this)

                  About the include thing. Would it be better to use HTML frames? Again, I don't want the files to be too big as I want to have some space left after a few users!

                  I hope you understand now. 🙂

                  BTW: forget that code I gave you, it dosen't matter now!

                  Thanks
                  Tom

                    you STILL dont want to use include, what you want for tunneling is simple file functions:

                    from manual:

                    <?php
                    $handle = fopen("http://www.example.com/a.txt", "rb");
                    $contents = '';
                    while (!feof($handle)) {
                    $contents .= fread($handle, 8192);
                    }
                    fclose($handle);

                    echo $contents;
                    ?>

                    but this will only output the same html code found in a.txt to the browser, this IS tunneling but it doesnt end here, you'll know what i'm talking about once you start developing..

                    you have to change all link URLS and URLS for pictures for this to work (any link that has a relative address will not work), and if pictures are absolute, they wont be tunneled, they will be opened directly from the other site by the browser..

                    happy coding!

                    oz

                      Write a Reply...