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.

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.

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.

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 :slight_smile:

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.

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

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


RewriteEngine On
RewriteOptions inherit

RewriteCond $1 !^(index\\.php|images|css|robots\\.txt|favicon\\.ico|Sitemap\\.xml|sitemap\\.xml)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L,NC,QSA]


index.php


<?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;



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:


RewriteEngine on
RewriteRule ^events/(.*)$ events/index.php?val=$1

It does display content:
http://www.realnewspost.com/events/index.php

but the variable is missing!
And astonishingly for:


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…

OIC!

[rant #1][indent]The use of “lazy regex,” specifically the :kaioken: EVERYTHING :kaioken: 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![/indent][/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