开发者

How to rewrite my URL to items/category/item-name-itemid using mod rewrite?

开发者 https://www.devze.com 2023-01-31 01:00 出处:网络
I want to rewrite my urls in this form: /items/category/item-name-id (the id is a number). Now my URL look like 开发者_如何学JAVAthis: items.php?cat=category&name=item-name&id=12321

I want to rewrite my urls in this form: /items/category/item-name-id (the id is a number). Now my URL look like 开发者_如何学JAVAthis: items.php?cat=category&name=item-name&id=12321

Oh and where I need to place the code for mod-rewrite ?


In .htacces:

RewriteEngine On

# Enable these for debugging, disable when done (it's "expensive")
# RewriteLog /var/log/apache2/rewrite.log
# RewriteLogLevel 9

# if the requested URI doesn't point to an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# .. pass it off to items.php (or index.php, or..) with the path as q,
# and whatever other GET parameters were there
RewriteRule ^(.*)$ items.php?q=$1 [QSA]

mod_rewrite needs to be enabled obviously, and Apache conf needs to give you
"AllowOverride Options"
or
"AllowOverride All"

Now the string "/items/category/item-name-id" will arrive in your script as GET parameter 'q', where you can split on '/' and '-' as required, do table lookups for each element etc.

I tend to pass the q array (which it is after splitting it) to the first object (Category class), which loads the second (Name class), which loads the third etc. But that depends entirely on what you're going to do with them.

0

精彩评论

暂无评论...
验证码 换一张
取 消