User-friendly URLs instead of Query Strings? [dupl

2020-03-24 07:39发布

Possible Duplicate:
using seo user friendly in php
Custom routing in code ingniter

I'm deving a CMS and I wanna use User-friendly URLs instead of Query Strings.

e.g.:

mysite.com/cat_1 => mysite.com/cat_1/itme_1

instead of

mysite.com/?cat=1 => mysite.com/?item=1

How should I implement this? any standard way?

My idea is to use of semi-dynamic pages and generating static snapshots with user-friendly URLS of dynamic pages that use query strings.

标签: php url
6条回答
ゆ 、 Hurt°
2楼-- · 2020-03-24 07:57

This is accomplished through the use of .htaccess to modify how the URL is sent to the client. Or as other people say, to prettify the URL. Here are some links to articles to help you get started with the mod_rewrite rules required to make this function.

http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html

查看更多
We Are One
3楼-- · 2020-03-24 07:58

You want to use .htaccess to rewrite your URLs internally and route them appropriately to your application.

Here's a specific .htaccess example that will do what you're looking for:

RewriteEngine on
RewriteRule ^cat_([0-9]*) /?cat=$1 [N,QSA]
RewriteRule ^item_([0-9]*) /?item=$1 [N,QSA]

Just put the above in a file called .htaccess in your root web folder, and you'll be good to go.

Note that you may have to enable mod-rewrite in your apache server if it's not already enabled (most shared hosting providers have it enabled)

查看更多
神经病院院长
4楼-- · 2020-03-24 08:09

How about using the MVC pattern for PHP?

http://www.tonymarston.net/php-mysql/model-view-controller.html http://oreilly.com/pub/a/php/archive/mvc-intro.html http://www.codeproject.com/Tips/401121/MVC-in-PHP

Arun

查看更多
戒情不戒烟
5楼-- · 2020-03-24 08:12

This functionality cannot be implemented using only PHP. You need to configure URL rewriting for your HTTP server, which will transform the user-friendly URLs into query strings which your scripts can understand. In the case of Apache, you should look into mod_rewrite, which can usually be configured through .htaccess files.

查看更多
放荡不羁爱自由
6楼-- · 2020-03-24 08:14

You can use a framework like Codeigniter: http://ellislab.com/codeigniter

And review: http://ellislab.com/codeigniter/user-guide/general/urls.html

You will need to activate the mod_rewrite and create the file .htaccess

.htaccess with Codeigniter:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1 [L]    
</IfModule>

Then you can use:

example.com/class/function/ID

I hope that is helpful.

Regards.

查看更多
小情绪 Triste *
7楼-- · 2020-03-24 08:17

use asp.net mvc you get it for free ;) but in all seriousness I think PHP has a web engine (code igniter)that provides advanced routing too. Also, you can role your own using some Apache black magic

查看更多
登录 后发表回答