我是.htaccess的新手,对它一无所知,只知道它改变了url。以下代码来自其他来源(没有第二行),它适用于其他人。我在这里要做的是,当我从index.php导航到profile.php时:
<html>
<li><a href="index.php">Profile</a></li>
<li><a href="<?php echo $user_data['Username'];?>">Profile</a></li>
<li><a href="settings.php">Profile</a></li>
<li><a href="about.php">Profile</a></li>
</html>
很明显,我想将用户名作为URL传递到他们的配置文件,以根据登录用户显示相关数据。它工作得很好,但如果我回到index.php或其他页面,它会说用户不存在。
这是我的profile.php文件:
if(isset($_GET['username']) === true && empty($_GET['username']) === false)
{
$username = $_GET['username'];
if(user_exists($username) === true)
{
$user_id = user_id_from_user($username);
$profile_data = user_data($user_id, 'First_Name', 'Last_Name', 'Email');
else
{
echo 'user doesn''t exist';
}
else
{
header('Location: index.php');
exit();
}
?>//Html部分名字:
姓氏:
电子邮件:
这是我的.htaccess代码:
重写引擎打开重写数据库/重写规则^index.php-[L,NS]重写结束%{ENV:REDIRECT_STATUS}^$重写结束%(REQUEST_FILENAME)-f重写结束%(REQUEST_FILENAME)-d重写规则^(.)$/Project/profile.php?用户名=$1
如果我删除第2行,我会得到一个错误:(服务器遇到内部错误或配置错误,无法完成您的请求)。事实上,我在不知道必须设置config-AllowOverride ALL的情况下尝试了上面的代码。之后,我在日志中只收到了这段代码的以下错误(我的意思是,只有当我删除第2行时)。这是我的错误日志:
[Sun Apr 03 13:27:44.876207 2016] [core:error] [pid 5512:tid 1684] [client ::1:14819] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Sun Apr 03 13:27:44.876207 2016] [core:error] [pid 5512:tid 1684] [client ::1:14819] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Sun Apr 03 13:51:23.628421 2016] [core:error] [pid 5512:tid 1684] [client ::1:15004] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Sun Apr 03 13:51:23.629398 2016] [core:error] [pid 5512:tid 1684] [client ::1:15004] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Sun Apr 03 13:51:25.422299 2016] [core:error] [pid 5512:tid 1684] [client ::1:15005] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Sun Apr 03 13:51:25.422299 2016] [core:error] [pid 5512:tid 1684] [client ::1:15005] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
访问日志:
正常情况下:
::1 - - [04/Apr/2016:03:18:10 +0530] "GET /project/dhanu HTTP/1.1" 200 2573 "http://localhost/project/index.php" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"
::1 - - [04/Apr/2016:03:18:20 +0530] "GET /project/dhanu HTTP/1.1" 200 2573 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"
删除.htaccess第二行时:
::1 - - [04/Apr/2016:03:17:58 +0530] "GET /project/index.php HTTP/1.1" 302 1060 "http://localhost/project/index.php" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"
::1 - - [04/Apr/2016:03:17:58 +0530] "GET /project/index.php HTTP/1.1" 302 1060 "http://localhost/project/index.php" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"
::1 - - [04/Apr/2016:03:17:58 +0530] "GET /project/index.php HTTP/1.1" 302 1060 "http://localhost/project/index.php" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"
::1 - - [04/Apr/2016:03:17:58 +0530] "GET /project/index.php HTTP/1.1" 302 1060 "http://localhost/project/index.php" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"
::1 - - [04/Apr/2016:03:18:08 +0530] "GET /project/index.php HTTP/1.1" 200 1311 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"
以下是我要做的工作:
.htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^.*index'.php$ - [L,NS]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^project/(.*)$ /project/profile.php?username=$1" [L,NS]
index.php
<?php
$user_data = [
'username' => 'dhanu'
];
?>
<html>
<body>
<li><a href="<?php echo $user_data['dhanu'];?>">Profile</a></li>
</body>
</html>
profile.php
<?php
// This is where you'd put a database query to check for the user
// this is just a sample that works for your case
function userExists($username) {
$users = ['thegraz', 'sudi', 'dhanu'];
return in_array($username, $users);
}
if(isset($_GET['username']) && $_GET['username'] !== '')
{
$username = $_GET['username'];
if (userExists($username))
echo 'User exists - ' . $username;
else
echo 'User doesn''t exist';
}
这样,只指向/project或/project/index.php的URL将只指向index.php
让我知道这是否适合你。