我想首先为我的措辞道歉。我在网络开发业务中没有正确地使用标题,所以就这样。
我使用的是Apache Server,我的项目根文件夹中有一个.htaccess文件。它指定了一个404页面,还指定了从URL:中的文件中删除.php的重写条件
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1'.php
</IfModule>
我的页面"social.php"上有一个用户部分,并使用代码中的GET变量检索他们。
示例URL:
www.mysite.com/users/social.php?username=someuser
然而,我想知道如何让它看起来像:
www.mysite.com/users/someuser
提前感谢!
让你的root.htaccess如下:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule ^users/('w+)/?$ /users/social.php?username=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME}'.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1.php [L]
</IfModule>
目前这只是mod_rewrite问题的一部分。
第一部分是将类似/users/[a-z]+
的URL路由到某个脚本/social.php
RewriteRule ^/users/(.*)$ social.php?username=$1
第二部分是在social.php中,您需要在$_GET['username']
中获得用户名"someuser"。