我的title已經(jīng)限定了字母和數(shù)字的,我想使用title自定義函數(shù)作為URL規(guī)則,
第一步、我在config/custom.php寫了一個函數(shù)titleurl
function titleurl($data) {
$title = str_replace(" ", "-", $data['title']);
return "/news/".$title.".html"; // 返回我自己的url規(guī)則:
}第二步、在url規(guī)則里面填寫新標(biāo)簽:
{modname}/{titleurl($data)}.html第三步、在config/rewrite.php,加入代碼
// 判斷url是否是來自自定義函數(shù)
if (CMSURI) {
$myfile = WRITEPATH.'myid/'.md5(urldecode(CMSURI)).'.txt';
if (is_file($myfile)) {
$id = file_get_contents($myfile);
if ($id) {
return [
CMSURI => 'index.php?c=show&id='.$id, // 這里寫內(nèi)容的地址
];
}
}
}第四步、然后更新內(nèi)容url地址,使url地址生效
第五步、編寫偽靜態(tài)解析規(guī)則,因為這是你自己寫的函數(shù),所以無法用系統(tǒng)生成的代碼了
"([\w]+)\/([\w]+)\.html" => "index.php?s=$1&c=show&title=$2",
為什么我訪問https://www.xxxx.cn/news/aaddb17339.html是404
訪問https://www.xxxx.cn/news/1.html可以。
是偽靜態(tài)解析規(guī)則寫錯了嗎還是其他原因
第一步函數(shù)寫的功能不全
參考文檔:《使用自定義函數(shù)作為URL規(guī)則》
function titleurl($data) { $title = str_replace(" ", "-", $data['title']); $url = "/news/".$title.".html"; // 返回我自己的url規(guī)則: dr_mkdirs(WRITEPATH.'myid/'); // 創(chuàng)建id緩存存儲目錄 file_put_contents(WRITEPATH.'myid/'. md5(trim($url, '/')).'.txt', $data['id']); // 將id號存儲緩存文件 return $url; }(以上回復(fù)內(nèi)容已被迅??蚣軇?chuàng)始人修改)
開源是一種精神,但不是義務(wù),幫忙是情分,不幫也不要抱怨,建議大家多研究代碼、多閱讀代碼、多翻閱社區(qū)歷史問題!
回復(fù)@外聘專員-GOLANG工作室 改成這個也是404,還有什么原因么?偽靜態(tài)解析規(guī)則有沒有問題?
function titleurl($data) { $title = str_replace(" ", "-", $data['title']); $url = "/news/".$title.".html"; // 返回我自己的url規(guī)則: dr_mkdirs(WRITEPATH.'myid/'); // 創(chuàng)建id緩存存儲目錄 file_put_contents(WRITEPATH.'myid/'. md5(trim($url, '/')).'.txt', $data['id']); // 將id號存儲緩存文件 return $url; }我偽靜態(tài)解析規(guī)則用下面這個可以用title當(dāng)url,但是這個時候3個URL都能訪問內(nèi)容頁面,想不用ID,只用title
https://www.xxxx.cn/news/show/1.html
https://www.xxxx.cn/index.php?s=news&c=show&id=1
https://www.xxxx.cn/news/aaddb17339.html