【问题标题】:php mysql .htaccessphp mysql .htaccess
【发布时间】:2023-03-13 22:00:02
【问题描述】:

我正在创建一个处理个人资料页面的网站,我需要从 URL 获取用户名,直到此时一切正常,但我在使用 .htaccess 时遇到了问题,我做到了不知道如何解决这个错误 这是我第一次使用 .htaccess 我正在使用 WAMP 包 在我添加 .htaccess 文件并隐藏包含它的文件夹后,浏览器给了我这个错误。

内部服务器错误 服务器遇到内部错误或配置错误,无法完成您的请求。 请通过 admin@localhost 联系服务器管理员,告知他们此错误发生的时间,以及您在此错误之前执行的操作。 服务器错误日志中可能会提供有关此错误的更多信息。 这是profile.php的一部分

profile.php

  // updatede for using the GET methode for ge the data nedded from the url 
    if(isset($_GET['u']))
    {
          $username = mysql_real_escape_string($_GET['u']);
          if(ctype_alnum($username))
         {


          //check user exist
             $check = mysql_query("SELECT user_name, first_name FROM user WHERE user_name = '$username' ")or die(mysql_error());
             if (mysql_num_rows($check)==1) 
             {
               $get = mysql_fetch_assoc($check);
               $username = $get['user_name']; 
               $fname = $get['first_name'];
             }
             else
             {
                echo "<h2>User does not Exist!!</h2>";
                exit();  
             }
          }
       }

        echo "$username";
?>      

.htaccess

RewriteEngine On

RewriteRule^([a-zA-Z0-9_-]+)$ profile.php?u=$1



RewriteRule^([a-zA-Z0-9_-]+)/$ profile.php?u=$1

【问题讨论】:

  • 您是否尝试在 RewriteRule 和您的重写规则之间添加空格?
  • 我正在使用我的移动浏览器,所以很难说,但我认为你的格式是错误的,在 ^ 之前 rewriterule 之后应该有一个空格
  • 是的,它们是这两行之间的空格 RewriteRule^([a-zA-Z0-9_-]+)$ profile.php?u=$1 RewriteRule^([a-zA-Z0- 9_-]+)/$ profile.php?u=$1
  • @user2172837 这不是你被问到的;这之间有很大的区别:RewriteRule^([a-zA-Z0-9_-]+)$ profile.php?u=$1 和这个:RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?u=$1(最后一个例子在RewriteRule 之后有一个空格)。
  • 我做了 - 两次!您需要在 .htaccess 文件中的 RewriteRule 和 ^([a-zA-Z0-9_-]+)$ profile.php?u=$1 之间留一个空格。空格 - 键盘底部的长按钮。

标签: php mysql .htaccess


【解决方案1】:

不要以 UTF8 字符编码保存您的 .htaccess 文件。保存在 ANSI 中

【讨论】:

  • 你为什么要这么做,我一直用utf-8没问题
  • @Sebas 我有这方面的经验。也许某些 apache 服务器有问题。
  • mmm 那么如何解决这个问题是我使用它的任何教程?因为直到现在我没有成功解决这个错误
  • @user2172837,只需将其保存为 ANSI 模式即可。希望这能解决您的问题。你可以使用windowsNotepad然后另存为,然后选择ANSI
  • @Amir 仍然是同样的错误 .. 文件的位置是否必须在某个特定路径中,或者是否因为包含 .htaccess 的文件夹被隐藏,我无法选择它
【解决方案2】:

假设 profile.php 没有错误。尝试删除 .htaccess 文件的最后一行。还要在 Rewriterule 和 '^' 之间添加一个空格。 由于 ctype_alnum() 函数,向正则表达式添加空格与此无关。这只允许字母字符。

只需将其复制并粘贴到您的 .htaccess 文件中并(当然)删除前面的行:

重写引擎开启
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?u=$1

【讨论】:

  • 我照你说的做了,但错误还是一样,文件夹仍然没有出现
猜你喜欢
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 2016-03-15
  • 1970-01-01
  • 2011-06-06
相关资源
最近更新 更多