我正在用PHP制作一个网站,我已经到了需要制作新闻页面的部分。
我已经编写了news.php
文件,并使用GET
来阅读要显示的文章的特定标题。这意味着我最终得到了一个像
/community/news.php?title=post-title
我更喜欢它像
/community/news/post-title
所以我查了一下,看到我可以使用.htaccess文件。
现在,我的实际.htaccess文件看起来与此类似
ErrorDocument 404 /notfound.php
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
# /community/news/post-title -> /community/news.php?title=post-title
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^community/news/([^/]*)$ /community/news.php?title=$1 [L]
</IfModule>
转到/community/news/post-title
的结果是404错误
非常感谢你的帮助。
答案 0 :(得分:0)
如果您在当地环境中工作,而您的网址就像http://localhost/website/community/news/post-title那么您应该拥有&#34;网站&#34;在&#34; RewriteBase /&#34;
RewriteBase /website
答案 1 :(得分:0)
因此,假设您的网址是example.com - (QSA标记将查询字符串字符串附加到网址)
RewriteEngine On
ErrorDocument 404 example.com/notfound.php
# /community/news/post-title -> /community/news.php?title=post-title
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^community/news/(.*)$ example.com/community/news.php?title=$1 [L,QSA]
答案 2 :(得分:0)
非常感谢你的帮助。
原来这是相对路径的问题。 它适用于:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
# /community/news/post-title -> /community/news.php?title=post-title
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/community/news/([^/]*)$ community/news.php?title=$1 [L]
</IfModule>
(差异在最后一行的'/')