ang*_*gus 6 php .htaccess url-rewriting
我在同一个目录中的两个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]
Run Code Online (Sandbox Code Playgroud)
现在我可以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;
}
Run Code Online (Sandbox Code Playgroud)
有什么帮助吗?
使用索引文件创建一个目录 posts(显示帖子列表,链接到漂亮的 url)和这个.htaccess:
RewriteCond %{REQUEST_URI} ^/posts/[^/]+/.*$
RewriteRule ^/posts/([^/]+)/$ postdetails.php?slug=$1 [QSA,L]
Run Code Online (Sandbox Code Playgroud)
漂亮的网址
in this pattern:
http://www.example.com/posts/slug/
e.g.:
http://www.example.com/posts/keywords-are-here/
Run Code Online (Sandbox Code Playgroud)
在文件postdetails.php中,您可以评估 param $_GET['slug']。
| 归档时间: |
|
| 查看次数: |
4942 次 |
| 最近记录: |