.htaccess URL重写不起作用而是显示404错误

时间:2015-03-30 16:56:19

标签: php apache .htaccess mod-rewrite

我希望使用.htaccess在我的网站上提供干净的网址我已经尝试了很多程序,但没有一个可以使用,这是我的.htaccess代码:

RewriteEngine on
RewriteRule ^post/([0-9a-zA-Z]+)$ post/?post=$1 [NC,L]

.htaccess文件位于我的实时服务器的/post/目录中。

我想实现的干净网址是http://example.com/news/post/latest-post/而不是http://example.com/news/post?post=latest-post

PHP代码:

$id = preg_replace('#[^0-9a-z_-]#i', '', $_GET['post']);

谢谢!

1 个答案:

答案 0 :(得分:0)

/news/post/.htaccess内部,此代码应该有效:

RewriteEngine on
RewriteBase /news/post/

RewriteRule ^([0-9a-z]+)/?$ ?post=$1 [NC,L,QSA]