创建友好的网址

时间:2015-06-03 14:41:22

标签: .htaccess

我整天都在苦苦挣扎。我尝试为我的localhost创建友好的URL。我的网站链接例如是www.example.com,当我想要查看我希望从www.example.com/news.php链接的新闻时看起来像www.example.com/news,我使用此代码执行此操作

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

一切正常但我也希望我的帖子和用户看起来不像www.example.com/users/?id=1或帖子www.example.com/posts/?id=3231我想要这样www.example.com/users/1或{{1}同时我的php文件没有.php扩展名。所以我基本上希望www.example.com/posts/3231www.example.com/somepage.phpwww.example.com/somepagewww.example.com/somepage.php?somevalue=something

1 个答案:

答案 0 :(得分:1)

您可以在根目录中的.htaccess中尝试此操作。

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(users|posts)/([^/]+)/?$ /$1/?id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?somevalue=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ $1.php [NC,L]