我在同一个目录中的两个PHP文件posts.php
和postdetails.php
.该posts.php
页面列出了所有帖子,并postdetails.php
在postid
设置时显示帖子:postdetails.php?id=3
我想在没有的情况下使posts
和postdetails
页面可访问,.php
并且还添加一个尾随正斜杠,如:
代替
www.domain.com/posts.php
我有:
www.domain.com/posts/
我用过这个:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^/]+)/$ $1.php RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$ RewriteRule (.*)$ /$1/ [R=301,L]
现在我可以posts.php
按照我想要的方式访问,如:www.domain.com/posts/
但现在所有帖子都显示链接为 www.domain.com/posts/postdetails.php?id=NUMBER
问题是我希望他们也喜欢他们在posts
子目录中,但我似乎无法使其工作.
我也想改变id
,以slug
所以,我有类似的东西,postdetails.php?slug=this-is-a-post
并将它改编为www.domain.com/posts/this-is-a-post/
我正在使用此功能来创建我的slu ..
function createSlug($str) { if($str !== mb_convert_encoding( mb_convert_encoding($str, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32') ) $str = mb_convert_encoding($str, 'UTF-8', mb_detect_encoding($str)); $str = htmlentities($str, ENT_NOQUOTES, 'UTF-8'); $str = preg_replace('`&([a-z]{1,2})(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i', '\\1', $str); $str = html_entity_decode($str, ENT_NOQUOTES, 'UTF-8'); $str = preg_replace(array('`[^a-z0-9]`i','`[-]+`'), '-', $str); $str = strtolower( trim($str, '-') ); return $str; }
有什么帮助吗?