Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP和htaccess的虚拟URL_Php_.htaccess - Fatal编程技术网

使用PHP和htaccess的虚拟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

我正在使用PHP开发一个应用程序,我希望我的用户能够使用自己的URL

以这种格式:

http://example.com/username

例如:

http://example.com/myCoolUsername

如果我现在使用此URL,服务器会认为它是一个文件夹,但我知道我应该以某种方式使用.htaccess文件,但我不知道应该在该文件中使用什么内容。

这假设您的index.php收到一个$\u 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]
这假设您的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