skip to Main Content

I am trying to write a .htaccess RewriteRule for a profile page. I want the rule to query a SQL database for a user profile and if that data returns a result to load the profile.php page. Example:

  • www.example.com/username
  • or www.example.com/user.name
  • or www.example.com/user.name123 and the like…

However, I have other pages like /login, /registration, /, /chat, and so on. Every time I try something I keep running into an issue.

I have tried the following:

RewriteEngine On
RewriteRule ^(login)$ login.php [QSA]
RewriteRule ^(logout)$ logout.php [QSA]
RewriteRule ^([w-]+)/?$ profile.php?pid=$1 [L,QSA]

I am trying to learn as much as I can but I am not comprehending the regex syntax and the conditions. The above rules let me use all the previous pages but it will catch everything else in the profile.php page. What am I doing wrong?

2

Answers


  1. Try This in your .htaccess file

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

    make sure that in profile.php you have put certain condition to match the parameters and if not matches it shows error message or redirect you to 404 page
    that would be nice

    Login or Signup to reply.
  2. Please try following regex, I hope it will solve your problem.

    ^([w]+|[w-]+.+[w]+)$
    

    you can always use this link to test your regex.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search