hello i needed to produce a format for customers url, for instance for that user ninja123, he'd obtain a profile link of example.com/ninja123, im using php mysql on apache. this is actually the normal link!
viewprofile.php?userid=2
how could i appraoch this issue? and im a poor .htaccesss newbie :)) thanks
Produce a .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
If your site customer demands http://example.com/Ninja123
, the folder will not be located therefore it will run index.php
and fill $_GET['url']
with /Ninja123
In index.php, grab the road component from $_GET['url']
:
$username = trim(parse_url($_GET['url'], PHP_URL_PATH), '/');
Now $username will contain Ninja123
. You should use that to perform a database search to retrieve the consumer id. If you would like this to utilize demands which contain sub-sites (example.com/Ninja123/photos/party) you will want to strip out individuals sub-sites before carrying out the query.
Rather than doing $_GET['userid']
and searching for ID... switch it by searching for title therefore it is like: viewprofile.php?userid=Ninja123
Do this Mod Rewrite generator: http://www.generateit.net/mod-rewrite/
You cannot make use of a .htaccess RewriteRule just for this. When the forum exposes database internal IDs, you've mostly to rewrite areas of the SQL query. Essentially you have to accept $_GET["username"]
rather than "userid"
first, after which rewrite the research. As example:
db_query("SELECT * FROM users WHERE id=?", (int)$_GET["userid"]);
to
db_query("SELECT * FROM users WHERE username=?", encode($_GET["username"]));
Is dependent around the application, the particular SQL table structure, and also the DB interface (your forum most likely uses mysql_query
and mysql_real_escape_string
rather than encode
).
Then you're able to deploy an easy RewriteCond !-f {%..}
and RewriteRule (.+) /viewprofile?username=$1
to obtain short Web addresses.