Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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中创建包含用户名的用户友好url_Php_.htaccess_Mod Rewrite_Url Rewriting_User Friendly - Fatal编程技术网

在php中创建包含用户名的用户友好url

在php中创建包含用户名的用户友好url,php,.htaccess,mod-rewrite,url-rewriting,user-friendly,Php,.htaccess,Mod Rewrite,Url Rewriting,User Friendly,我想在url中传递用户名,就像facebook等其他社交网站一样。 url应该是这样的:www.mysite.com/username 另外,如果用户名处的值是目录名,我希望能够访问目录 例如,www.mysite.com/downloads将进入downloads目录 为此,我可以做的是首先检查传递的值是否是有效的目录名。如果是,access directory else检查是否存在传递用户名的用户 为此,我在根目录中创建了一个.htaccess文件,其中包含 RewriteEngine On

我想在url中传递用户名,就像facebook等其他社交网站一样。
url应该是这样的:
www.mysite.com/username

另外,如果用户名处的值是目录名,我希望能够访问目录

例如,
www.mysite.com/downloads
将进入
downloads
目录

为此,我可以做的是首先检查传递的值是否是有效的目录名。如果是,access directory else检查是否存在传递用户名的用户

为此,我在根目录中创建了一个
.htaccess
文件,其中包含

RewriteEngine On
RewriteRule ^ index.php
并检查url

// requested url
$request = $_SERVER['REQUEST_URI'];
// trim last / if available
$url = rtrim($request,'/');
// explode url and save in $ar array
$ar = explode('/',$url);

if(sizeof($ar) > 2)
{
  header('location : error/?error=1');
} else {
  $user = $ar[1];
}
上面的if语句用于检查传递的参数是否为一个或多个。 例如,
www.mysite.com/username/post
将导致无效的url

但是通过这段代码,我无法访问我的目录
www.mysite.com/downloads
做这件事最简单的方法是什么

编辑2:


我希望url
www.mysite.com/username
作为变量传递
username
,如果它不是
myProfile
目录中的
index.php
目录或文件,则它作为
$user=$\u GET['u']访问

更新了
.htaccess

# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
Options All -Indexes

# Timezone
SetEnv TZ Asia/Kolkata

ErrorDocument 404 /404.html
ErrorDocument 403 /403.html

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /myProfile/index.php?u=$1 [L,QSA]

/.htaccess
中尝试此代码:

ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
DirectoryIndex index.php

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ myProfile/index.php?u=$1 [L,QSA]

如果您仍然获得
404
,请将相同的垃圾(随机)文本放在
/.htaccess
顶部,并查看它是否生成500(内部服务器),以验证您的
.htaccess
是否已启用当您在浏览器中访问
www.mysite.com/index.php?u=username
URL时是否出错。

感谢@Henders共享链接,但在那里遵循答案,这可能是重复的,它给出了
500内部服务器错误
@anubhava我已经更新了
.htaccess
,还有更多的描述。
www.mysite.com/username
给出了
404
错误,并显示了
404错误页面
。htaccess
位于根目录ie中。,在
www.mysite.com
上,包含一个子目录
myProfile
和一个
index.html
页面。是的
www.mysite..com/myProfile/index.PHP?u=username
正在从浏览器中运行这是正常的,但它正在
www.mysite.com/myProfile/username
上运行,而不是
www.mysite.com/username