My url is http://testurl.com/user.php?id=1&name=TestUser
My default url for above link would need to be like http://testurl.com/user/12345/TestUser
If user try to change it in own browser link like below
http://testurl.com/user/12345
http://testurl.com/user/12345/
http://testurl.com/user/12345/TestUsers_bla-bla
then url bar automatically changes to http://testurl.com/user/12345/TestUser
Edit
<?php
$name = "This-is-a-test-user";
$id = 1;
$fields = array('id' => $id,
'name' => $name);
$url = "http://localhost/view_user.php?" . http_build_query($fields, '', "&");
?>
<a href = "<?php echo $url; ?>"> View User</a>
Why not try to get a very basic rule working, then go from there.
First you need to make sure you server has the rewrite module enabled, then at the top of your htaccess file put:
Then you can put your re-write rules using regex. Eg:
You need two things here.
1) htaccess rules to handle
http://testurl.com/user/12345
http://testurl.com/user/12345/
http://testurl.com/user/12345/xxx
and corresponding rules to avoid duplicate content (redirect old format
/user.php?xxx
to new format/user/ID/NAME
)To do so, you can put this code in your root htaccess
Note: at this point, make sure mod_rewrite is enabled and
htaccess
allowed (in Apache configuration file).A simple test:
http://example.com/user/12345/XXXXX
should internally rewrite to/user.php?id=12345&name=XXXXX
.2) Now you need to adapt
user.php
logic (this is here you check for your data<ID, NAME>
pair existence)I intentionally write this code with "duplicate" parts to let you understand the logic.
Of course, you can improve it.
Try this in root/.htaccess file
The conditions make sure you don't rewrite any existing file/directories on the server.