.htaccess url rewrite querystring

2019-05-23 01:27发布

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

2条回答
Luminary・发光体
2楼-- · 2019-05-23 01:53

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)

查看更多
可以哭但决不认输i
3楼-- · 2019-05-23 01:58

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

查看更多
登录 后发表回答