2

Ok I've read and search some here but haven't found an answer for my question or maybe im too new to url rewrite and just can't figure it out. Here's what I am trying to accomplish:

I have a table that holds channels description and ID, the ID is used to know what content to show, so I have something like this in my URL

http://www.mysite.com/page?channel=1

now what I would like to do is to display this:

http://www.mysite.com/page/description

and still be able to somehow get the id of that description to display the appropriate content. hopefully this makes sense. The only thing that i've thought of is at the top of my page, do a:

select * from channels where description = $_GET['description']

and have that return the id, and then use it. Would that be the only way to go? or is .htaccess good enough? such a noob :(

EDIT: this is in my htaccess right now:

AddType x-mapp-php5 .php
Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
##This is to redirect just stream page only
RewriteRule ^stream/(.+)/([0-9]+)$ /stream.php?channel=$1
1
  • look at my answer here Commented Feb 12, 2013 at 7:41

2 Answers 2

1

If you want to get ID, you shell do next:

  1. Enable mod_rewrite the apache module
  2. Create .htaccess in the DocumentRoot directory
  3. Paste next:

    RewriteEngine On
    RewriteRule ^([0-9]+)$ /page.php?channel=$1
    
  4. When you follow the URL:

http://www.mysite.com/1

you get content like from URL:

http://www.mysite.com/page?channel=1

Sign up to request clarification or add additional context in comments.

6 Comments

this would work except that I prefer not to show the actual "ID" value "1" but it's description instead, is that possible? or would the url have to look like mysite.com/page/description/1?
change ^([0-9]+)$ /page.php?channel=$1 to ^page/description/([0-9]+)$ /page.php?channel=$1
"description" is dynamic too, so would it be "^page/$1/([0-9]+)$ /page.php?channel=$2"?
^page/(.+)/([0-9]+)$ /page.php?channel=$2
look at my edit, i added what i have currently in my htaccess file, when adding your code it breaks and i get a page not found error
|
0

There are a number of ways of doing this.

Using a MVC architecture in your application will force you to use a routing logic in one form or another.

What you specifically want can be done in a number of ways, including apache RewriteMap.

I'd personally design my application in such a way that it receives the URI's and routes them to a controller/page. This ensures that PHP continues to have full control over what is finally displayed for a request.

Assuming you are given the task of beautifying your links without affecting much of the application, you need to find a way to map /page/description to /page?channel=1.

For this unique situation, I would use something similar to a decorator pattern, because in its essence your task does not need to modify the existing codebase:

.htaccess

RewriteEngine On
RewriteRule ^.*$ router.php [NC,L]

router.php

include'config.php';// remove your config loading from index.php

$request = $_SERVER['REQUEST_URI'];
$request = explode('/', $_request);
    # in $request you will have all the uri segments now

/* complex logic to find out what page you're on */

   # assuming we found out it's page/description
$_GET['channel'] = 1;// or getIdFromDescription($request[2])
   # this forces the environment to believe it got a request with ?channel=1

# revert to your old request with injected environment
include'index.php';

The premise here is that index.php can load your old pages. And you just need to change the way they are accessed. So you use a router to map the new request to the old request.

It's not perfect, but works well on poorly designed applications.

TAKE NOTE: My current implementation does not handle static resources (images, css, js, etc)

4 Comments

I get most of this, my question in router.php is i see you're hardcoding the $_GET['channel'] = 1, but that value changes like if its page/differentdescription would equal to channel=3, know what i mean?
// or getIdFromDescription($request[2]) I can't implement the function for you, please read the comments to understand what you need to do to make this fully functional.
ohh im sorry, for some dumb reason i was reading it like a javascript code getid...sorry
Updated my comments a bit, consider any comment with # to be an actual comment explaining the logic, and all other comments directions for you on how to get it to work :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.