1

i am creating a website that handle a profile page and i need to get the username from the URL until this point all work fine but i am having a problem with using the .htaccess and i do not know how to fix this error this is my first time to use the .htaccess i am using the WAMP package and the browser give me this error after i add the .htaccess file and it hide the folder that contain it.

Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at admin@localhost to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log. this is a part of the 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
user2172837
  • 39
  • 1
  • 6
  • Did you try adding whitespaces between `RewriteRule` and your rewrite rules? – Repox Mar 17 '13 at 20:08
  • I'm using my mobile browser so it's hard to tell, but I think your formatting is wrong there should be a space after rewriterule before the ^ – StrikeForceZero Mar 17 '13 at 20:11
  • yes their is a space between these 2 lines RewriteRule^([a-zA-Z0-9_-]+)$ profile.php?u=$1 RewriteRule^([a-zA-Z0-9_-]+)/$ profile.php?u=$1 – user2172837 Mar 17 '13 at 20:15
  • @user2172837 That's not what you are being asked; there's a big difference between this: `RewriteRule^([a-zA-Z0-9_-]+)$ profile.php?u=$1` and this: `RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?u=$1` (the last example having a whitespace after `RewriteRule`). – Repox Mar 17 '13 at 20:17
  • @ – Repox sir this the first time i use the .htaccess i do not understand what you are saying can you explain – user2172837 Mar 17 '13 at 20:19
  • 1
    I did - two times! You need to have a space between `RewriteRule` and `^([a-zA-Z0-9_-]+)$ profile.php?u=$1` in your .htaccess file. A space - the long button in the bottom of your keyboard. – Repox Mar 17 '13 at 20:27
  • off topic, but it's worth knowing that the `mysql_xx()` functions are deprecated. If at all possible, I recommend switching to the `mysqli` or `PDO` libraries instead. – Spudley Mar 17 '13 at 20:32
  • you probably need a rewritecond on the {REQUEST_URI} – Sebas Mar 17 '13 at 20:55
  • @ Repox i have a space between RewriteRule and ^([a-zA-Z0-9_-]+)$ profile.php?u=$1 but still having error and the folder is still hiding – user2172837 Mar 17 '13 at 21:07

2 Answers2

1

Do not save your .htaccess file in UTF8 charcter encoding. save it in ANSI

Amir
  • 4,089
  • 4
  • 16
  • 28
  • why would you do such a thing, i've always used utf-8 without any problem – Sebas Mar 17 '13 at 20:54
  • @Sebas I had experience about it. perhaps some apache servers have problem with it. – Amir Mar 17 '13 at 20:56
  • mmm so how to solve this is that any tutorial that i use it ?? be cause until now i did not get success with this error – user2172837 Mar 17 '13 at 21:01
  • @user2172837, just save it in ANSI mode.hope this solve your problem. you can use windows `Notepad` and then save as, then select ANSI – Amir Mar 17 '13 at 21:04
  • @Amir still the same error .. does the place of the file must be in some specific path or no because the folder that hold .htaccess it is hidding and i can not choose it – user2172837 Mar 17 '13 at 21:16
  • @user2172837, it usually should be in root – Amir Mar 17 '13 at 21:17
  • it is in the root folder and this folder and not appear as it is hide – user2172837 Mar 17 '13 at 21:25
  • thank you AMIR you saved me but it do not make any sense what is the difference between the type of encoding ?? – user2172837 Mar 17 '13 at 21:28
  • @user2172837, There is some explanation about difference http://stackoverflow.com/questions/700187/unicode-utf-ascii-ansi-format-differences Also you can google it...some hiding characters cause server to error – Amir Mar 17 '13 at 21:32
0

Assuming that profile.php has no errors. Try to delete your last line of the .htaccess file. Also add a space between Rewriterule and '^'. Adding whitespace to the regex has nothing do to with this, because of the ctype_alnum() function. This only permits alphabetic characters.

Just copy and pase this to your .htaccess file and (ofcourse) delete the previous lines:

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

Bob Dolman
  • 31
  • 9