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

*爱你&永不变心* 提交于 2019-12-20 07:30:35

问题


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

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


回答1:


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.



来源:https://stackoverflow.com/questions/4477650/how-to-rewrite-my-url-to-items-category-item-name-itemid-using-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!