url Slug php and htaccess issue


url Slug php and htaccess issue

我正试图创建一个段塞,以标题作为段塞从主页跳转到文章页面。

这是我的.htaccess代码:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^post/([a-zA-Z0-9_]-)/([0-9]+)'$ article.php?id_art=$1 [NC,L]

这是我的php链接:

<a href='article/post/$id_art/$slug'><button class='read-more-btn'>Read more</button></a>

url中的slug看起来非常像这样:

http://localhost/sitename/article/post/8/the-slug-i-want-to-appear

但我得到了这个结果:

找不到对象
错误404

伙计们,有什么问题?我想是.htaccess.help请

您的正则表达式是向后的。您有postIDslug,但正则表达式匹配postslug(数字/字母/下划线)和ID。尝试:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^post/([0-9]+)/([a-zA-Z0-9_-]+)/?$ article.php?id_art=$1 [NC,L]

请注意,您应该已经得到了表达式[a-zA-Z0-9_]-的错误,这是无效的。

这可能是因为你有一个匹配^post/的规则(但你没有文章的规则,请尝试RewriteRule ^article/post/([a-zA-Z0-9_]-)/([0-9]+)'$ article.php?id_art=$1[NC,L]`,看看这是否有帮助。

您还有sitename,它不在规则中。您试图用链接中的article.php重写的原始url是什么?试着把它也包括在内。