使用PHP和htaccess的虚拟URL
我正在使用PHP开发一个应用程序,我希望我的用户能够使用自己的URL 以这种格式: http://example.com/username 例如: http://example.com/myCoolUsername 如果我现在使用此URL,服务器会认为它是一个文件夹,但我知道我应该以某种方式使用.htaccess文件,但我不知道应该在该文件中使用什么内容。这假设您的index.php收到一个$\u GET['username']映射到其URL:使用PHP和htaccess的虚拟URL,php,.htaccess,Php,.htaccess,我正在使用PHP开发一个应用程序,我希望我的用户能够使用自己的URL 以这种格式: http://example.com/username 例如: http://example.com/myCoolUsername 如果我现在使用此URL,服务器会认为它是一个文件夹,但我知道我应该以某种方式使用.htaccess文件,但我不知道应该在该文件中使用什么内容。这假设您的index.php收到一个$\u GET['username']映射到其URL: RewriteEngine On # Make
RewriteEngine On
# Make sure you only match on files/directories that don't actually exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite the first part after the `/` into a `username` parameter
RewriteRule ^(.+)$ /index.php?username=$1 [L,QSA]
这假设您的index.php收到一个$_GET['username']以映射到其URL:
RewriteEngine On
# Make sure you only match on files/directories that don't actually exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite the first part after the `/` into a `username` parameter
RewriteRule ^(.+)$ /index.php?username=$1 [L,QSA]
这条规则将被改写http://domain.com/username 以用户名作为GET参数名访问user.php
要进行测试,请在根目录中创建一个名为user.php的文件,并将其放入文件中:
通路http://domain.com/blablaName123 如果一切正常,您将看到string13 blablaName123此规则将被重写http://domain.com/username 以用户名作为GET参数名访问user.php
要进行测试,请在根目录中创建一个名为user.php的文件,并将其放入文件中:
通路http://domain.com/blablaName123 如果一切正常,你应该看到string13 Blaname123