SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How does one direct people from a simple URL to destination?

    Hi,

    I want for people when entering the simple URL of:

    www.realnewspost.com/events.chs

    to be redirected to:

    www.realnewspost.com/events/index.php?event=chs

    I have set up this .htaccess file in the root dir of this web site:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^events/(.*)$ events/index.php?name=$1


    but it is not redirecting!
    What does one need to do to get the above simple URL to map into the complete complex URL?

    ThanX.

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How does one convert a short URL to a Php based long URL?

    Hi,

    I want for people when entering the simple URL of:

    www.realnewspost.com/events.chs
    or
    www.realnewspost.com/events/chs

    to be redirected to:

    www.realnewspost.com/events/index.php?event=chs

    So that one can then grab the value of event via of course $_GET['event']

    What does one need to do to get the above simple URL to map into the complete complex URL?

    ThanX.

    Anoox search engine volunteer

    www.anoox.com

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,150
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    This is usually done using apache's mod_rewrite. I've moved your thread to the Server and Configuration area, as that is where you'll find your answer.

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,838
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by WorldNews View Post
    Hi,

    I want for people when entering the simple URL of:

    www.realnewspost.com/events.chs
    or
    www.realnewspost.com/events/chs

    to be redirected to:

    www.realnewspost.com/events/index.php?event=chs

    So that one can then grab the value of event via of course $_GET['event']

    What does one need to do to get the above simple URL to map into the complete complex URL?

    ThanX.
    PHP Frameworks approach is to redirect every URL to index.php and to use a router before applying the header() function to route to the required controller.

    The benefit of this approach is that it is easier to modify the router and to test using breakpoints.

    Now waiting for the flak from the Apache Gurus
    Last edited by John_Betong; Apr 25, 2013 at 18:00. Reason: spelling: corrected auto speller
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  5. #5
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This does not answer my Question at all!

    If you had read my Question, I am talking about that for reasons too many to describe reason, but mainly aesthetic reasons
    we need to give people a URL like this to visit:


    www.realnewspost.com/events.chs
    or
    www.realnewspost.com/events/chs

    where NO php is present in the URL and the index.php cannot know what the name of the variable is.

    So again the Question is how does one redirect the URL like above to its full version which for
    example would be:

    www.realnewspost.com/events/index.php?event=chs


    I am pretty sure this needs to be done via .htaccess code, but not exactly sure how.

    Anoox search engine volunteer

    www.anoox.com

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by WorldNews View Post
    /events.chs => /events/index.php?event=chs

    I have set up this .htaccess file in the root dir of this web site:

    Code:
    # Options +FollowSymLinks - This should already be in the httpd.conf
    RewriteEngine on
    # RewriteBase / - not necessary
    RewriteRule ^events/(.*)$ events/index.php?event=$1
    That should give you the redirection you've stated that you're looking for (assuming that events and event are static and not variable).

    You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It's helped may members and should help you, too.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,838
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by WorldNews View Post
    This does not answer my Question at all!

    If you had read my Question, I am talking about that for reasons too many to describe reason, but mainly aesthetic reasons
    we need to give people a URL like this to visit:


    www.realnewspost.com/events.chs
    or
    www.realnewspost.com/events/chs

    where NO php is present in the URL and the index.php cannot know what the name of the variable is.

    So again the Question is how does one redirect the URL like above to its full version which for
    example would be:

    www.realnewspost.com/events/index.php?event=chs


    I am pretty sure this needs to be done via .htaccess code, but not exactly sure how.
    This is what I had in mind:

    Try these links:

    http://subdomain.johns-jokes.com/tom
    http://subdomain.johns-jokes.com/dick
    http://subdomain.johns-jokes.com/harry

    http://subdomain.johns-jokes.com/not-permitted

    http://subdomain.johns-jokes.com/events.tom
    http://subdomain.johns-jokes.com/events.dick
    http://subdomain.johns-jokes.com/events.harry

    http://subdomain.johns-jokes.com/events.not.permitted

    # .htaccess - ALL URIs routed to index.php
    PHP Code:
    RewriteEngine On
    RewriteOptions inherit

    RewriteCond 
    $!^(index\.php|images|css|robots\.txt|favicon\.ico|Sitemap\.xml|sitemap\.xml)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond 
    %{REQUEST_FILENAME} !-d
      RewriteRule 
    ^(.*)$ index.php/$[L,NC,QSA
    # index.php
    PHP Code:
    <?php 
    #error_reporting(-1); ini_set('display_errors',1);
    error_reporting(0);

    $site 'http://www.realnewspost.com';
    $URI  =  substr($_SERVER['REQUEST_URI'],1);
    $URI  =  strtolower($URI);
    switch(
    $URI):
      case 
    'events.chs':
      case 
    'events.tom':
      case 
    'events.dick':
      case 
    'events.harry':
          
    $chs strstr($URI'.');
          
    $chs substr($chs,1);
          
    header
          
    (
          
    "Location: $site.'/events/index.php?event=' .$chs
          
    );
        exit;
      break;

      case 
    'events/chs':
          
    header
          
    (
            
    "Location: $site.'events/index.php?event=chs'
          
    );
        exit;
      break;

      case 
    'tom':
      case 
    'dick':
      case 
    'harry':
      break;

      default:
        echo 
    '<p>$URI NOT Recognised: ' $URI .'</p>';
    endswitch;

      
    $aPermitted = array
      (
        
    'events.chs',
        
    'events/chs',
        
    #'books',
        #'stickman',
        
    'tom',
        
    'dick',
        
    'harry',
      );
    $sp='http://www.sitepoint.com/forums/showthread.php?1044537-How-does-one-convert-a-short-URL-to-a-Php-based-long-URL&p=5410238#post5410238';  
    if( 
    in_array($URI$aPermitted)):
      echo 
    '<h3>' .'<a href="' .$sp .'">Sitepoint Thread:</a></h3>';
      echo 
    '<p>Finish switch(' .$URI .')</p>';
      echo 
    '<p>Permitted $URI\'s: ' .$URI .'</p>';
      echo 
    '<pre>';
        
    print_r($aPermitted);
      echo 
    '</pre>';
    endif;  
    exit;
    Last edited by John_Betong; Apr 25, 2013 at 22:45. Reason: added .htacces file
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  8. #8
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    No, the events of course are variable.

    So for example if original URL is:

    www.realnewspost.com/events/chs

    The "chs" is the name of the event, that is variable.

    So what I need to know is how to then go from one of these (clean & easy to type in URLs) to:

    www.realnewspost.com/events/index.php?event=chs

    So when I use what you suggested in the .htaccess file in root dir of this Web site:

    PHP Code:
    RewriteEngine on
    RewriteRule 
    ^events/(.*)$ events/index.php?val=$
    It does display content:
    http://www.realnewspost.com/events/index.php

    but the variable is missing!
    And astonishingly for:
    PHP Code:
    if (isset($_GET['val'])) {
        
    $val $_GET['val'];
            echo 
    $val;

    it prints out"index.php" for that value!
    Last edited by John_Betong; Apr 27, 2013 at 20:29. Reason: John_Betong - added PHP code tags

    Anoox search engine volunteer

    www.anoox.com

  9. #9
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,838
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by WorldNews View Post
    Hi.

    No, the events of course are variable.

    So for example if original URL is:

    www.realnewspost.com/events/chs

    The "chs" is the name of the event, that is variable.

    So what I need to know is how to then go from one of these (clean & easy to type in URLs) to:

    www.realnewspost.com/events/index.php?event=chs

    So when I use what you suggested in the .htaccess file in root dir of this Web site:

    PHP Code:
    RewriteEngine on
    RewriteRule 
    ^events/(.*)$ events/index.php?val=$
    It does display content:
    http://www.realnewspost.com/events/index.php

    but the variable is missing!
    And astonishingly for:
    PHP Code:
    if (isset($_GET['val'])) {
        
    $val $_GET['val'];
            echo 
    $val;

    it prints out"index.php" for that value!
    http://subdomain.johns-jokes.com/

    I have updated the subdomain test site with:

    1. left column with possible test links.
    2. htaccess source code
    3. index.php source code
    4. added CHS delimiter array('-', '.', '_');

    Although the index.php is verbose at 42 lines it is easily modified for additional links as opposed to a cryptic .htaccess file.

    I prefer to be able to run the script, stop at breakpoints and examine variables, adjust script then try again...
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  10. #10
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    OIC!

    [rant #1]
    The use of "lazy regex," specifically the EVERYTHING atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is "greedy." Unless you provide an "exit" from your redirection, you will ALWAYS end up in a loop!
    [/rant #1]

    Specify that your val value will be lowercase letters only (to avoid looping on index.php). Did I really do that? Horrors!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


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
  •