-2

I am having a url like

I need to change this url to

This username is taken dynamically from the database with the entry id=1064

Please advice me a solution to rewrite my url to the above format

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • 1
    Possible duplicate of [PHP Application URL Routing](http://stackoverflow.com/questions/125677/php-application-url-routing) – Ruslan Osmanov May 04 '16 at 06:10

2 Answers2

0

You can use RewriteEngine on in your .htaccess file to add RewriteRules. Have a look at Mod Rewrite:

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Luke Joshua Park
  • 9,527
  • 5
  • 27
  • 44
0

First of all, you have to have RewriteEngine on inside your .htaccess.

RewriteEngine On

After that, you need the regular expression for your link. Supposing that all your username contains only letters and numbers, this will be the appropriate regular expression. However, if it's not the case, simply adjust the [A-z0-9] part to whatever characters your username can have.

RewriteRule ^([A-z0-9])/?$ profile.php?username=$1 [NC,L]

Notice that we're passing the username as the $_GET param instead of the id.

Also, it's recommended to put something before the username in your link. In other words, http://example.com/u/username instead of http://example.com/username because if you have a page such as http://example.com/random, it's going to call the profile.php with random as the username. So for the example above, the regular expression would be:

RewriteRule ^u/([A-z0-9])/?$ profile.php?username=$1 [NC,L]
Chin Leung
  • 14,621
  • 3
  • 34
  • 58