我整天都在苦苦挣扎。我尝试为我的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/3231
到www.example.com/somepage.php
和www.example.com/somepage
到www.example.com/somepage.php?somevalue=something
答案 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]