我有一个类似的url
fullarticle.php?sID=3&ssID=21&id=1496
sID = category (Forums, entertaiment, news, sports etc)
ssID = sub category (businnes, health, crime, etc)
id = article id
我的目标是让这个url看起来像这个
fullarticle/News/Crimes/title
我在.htaccess
:上试过这个
RewriteRule ^([^/]+)/([^/]+)/([^'.]+)/([^'.]+)/([^'.]+)/([^'.]+)$ fullarticle.php?$1=$2&$3=$4&$5=$6 [L,NC,QSA]
另一个
RewriteRule ^/([a-zA-Z'+]+)/([a-zA-Z'+'-]+)/([0-9]+)$ fullarticle.php?sID=3&ssID=21&id=1496
还有一些运气不佳。
我如何编写规则/条件来实现这一点?如果有更简单的方法,我愿意接受建议。
感谢您花时间对此进行调查。
您可以尝试RewriteMap
从名称映射到数字id。但这需要在之前设置这样的地图,例如
categories.txt:
news 1
sports 2
entertainement 3
subcategories.txt:
crime 11
baseball 21
football 22
movie 31
titles.txt:
rewrite-rules-solved 83
stackoverflow-rocks 77
you-are-the-greatest 27
然后在RewriteRule
中使用
RewriteMap cat "txt:/path/to/categories.txt"
RewriteMap sub "txt:/path/to/subcategories.txt"
RewriteMap title "txt:/path/to/titles.txt"
RewriteRule ^/(.*?)/(.*?)/(.*)$ fullarticle.php?sID=${cat:$1}&ssID=${sub:$2}&id=${title:$3} [L]
您也可以使用prg
或dbd
映射来代替txt
文件。这将允许从程序或SQL select语句中检索名称到id的映射。有关更多详细信息,请参阅RewriteMap
手册。
我最终做的是像这样构建我的动态URL:
fullarticle/3/21/1496/title.html
其中3=类别,21=子类别,1496=标题id
我的重写如下:
RewriteRule ^fullarticle/([^/]+)/([^/]+)/([^'.]+)/([a-zA-Z0-9-]+).html$ fullarticle.php?sID=$1&ssID=$2&id=$3 [L,NC,QSA]
我是个新手,只花了6个小时就弄明白了。我为自己感到骄傲!