Php 如何使用mod rewrite将我的URL重写为items/category/item name itemid?
我想在此表单中重写我的URL:/items/category/item name id(id是一个数字)。现在我的URL看起来像这样:items.php?cat=category&name=item name&id=12321 哦,我需要把mod rewrite的代码放在哪里?在.htacces:Php 如何使用mod rewrite将我的URL重写为items/category/item name itemid?,php,url,mod-rewrite,Php,Url,Mod Rewrite,我想在此表单中重写我的URL:/items/category/item name id(id是一个数字)。现在我的URL看起来像这样:items.php?cat=category&name=item name&id=12321 哦,我需要把mod rewrite的代码放在哪里?在.htacces: RewriteEngine On # Enable these for debugging, disable when done (it's "expensive") # RewriteLog /v
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,Apache conf需要为您提供“允许超越选项”
或
“允许超越所有” 现在字符串“/items/category/item name id”将作为GET参数“q”到达您的脚本中,您可以根据需要在“/”和“-”上拆分,为每个元素进行表查找等
我倾向于将q数组(拆分后的数组)传递给第一个对象(类别类),然后加载第二个对象(名称类),然后加载第三个对象,等等。但这完全取决于您将如何处理它们。juat在代码中重写它,将items.php?cat=category&name=item name&id=12321更改为/items/category/item-name-id。当然,您需要使用一些重写规则对其进行备份。