0

At the moment I am developing a website with user interaction. I could transform all of the usernames in links for the profile page with a file .htaccess that has the following commands:

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

If I understand correctly, this code substitutes the "profile.php" for the argument after the signal "?" if the URL matches the regular expression. But I didn't understood how the "u=$1" accomplishes that because the "u" is not a variable session. This is my configuration in PHP for the session variables:

$_SESSION["id"] = $id;
$_SESSION["username"] = $username;
$_SESSION["password"] = $pswd;
$_SESSION["email"] = $email;

I would like to include too space in the regular expression, because at the moment only usernames like "FirstUser" are accepted, user names like "First User" give me an error at the browser.

When I add a space in the pattern I get the following HTTP 500 error:

Server Error

Adam Katz
  • 14,455
  • 5
  • 68
  • 83
Kasbah
  • 47
  • 1
  • 10

1 Answers1

0

Your .htaccess config

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

will try to load the rewrite engine, which may not be allowed (check this part first!) and (if successful) will then take matches under the .htaccess file's structure and convert them to profile.php with an HTTP GET request using variable u set to a value equal to that sub-path.

So if the .htaccess file lives at /home/kasbah/public_html and corresponds to http://foobar/~kasbah, then when a user goes to http://foobar/~kasbah/it-works, they will be redirected to http://foobar/~kasbah/profile.php?u=it-works.

Your PHP code can then call something like $_GET['u'] in order to act on that variable.

Escape spaces as %20. You can also make the final / optional and have only one RewriteRule:

RewriteEngine on
RewriteRule ^((?:[a-zA-Z0-9_-]|%20)+)/?$ profile.php?u=$1
Adam Katz
  • 14,455
  • 5
  • 68
  • 83