mod rewrite

I am trying to have a unquie address for each member at www.domain.com/username

So I know how to do this manually but I cant store my entire database of usernames in a mod rewrite file.

How could i make this work?
blink10Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arober11Commented:
What are you trying to do?
0
blink10Author Commented:
making everyone a profile page with a custom url...like how you can do on facebook
0
Ray PaseurCommented:
Maybe you could build your application as a Facebook app.  Then you would get the power of Facebook networking.

Another option might be to have a "translator" page.  It would be a 404 handler that  would recognize the natural name in the URL and load the corresponding web page script.  You can have a data base table that correlates the program script names with the natural names.
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

blink10Author Commented:
ok so where do I learn how to make this 404 translator page?
0
Ray PaseurCommented:
I'll try to give you an example.  It may take a few monutes...
0
Ray PaseurCommented:
Have a look at this code snippet.  I'll try to offer some explanation.  This code assumes you have made a connection to the data base server!

Lines 6-12 help us simulate a data base.  This is just for testing.

Lines 14-15 get the end of the URL and normalize to upper case.  So if we had something like www.example.com/blink10 the resulting variable would be BLINK10

Line 9 would look more like this (a lookup on the user name to translate the user):
$sql = 'SELECT script_name FROM associated_pages WHERE user_name = '$nom' LIMIT 1;
$res = mysql_query($sql);
if (mysql_num_rows($res) == 0)

Line 26-29 would look more like this
$row = mysql_fetch_assoc($res);
$sfn = $row["script_name"];
include_once($sfn);

Given the code here, your .htaccess file might say something like this.
ErrorDocument 404 /RAY_temp_404_handler.php

This error handler script would be stored in the www Root.

HTH, ~Ray
<?php // RAY_temp_404_handler.php
error_reporting(E_ALL);

// A 404 HANDLER THAT TRANSLATES A URL INTO A SCRIPT-FETCH SIGNAL

// SIMULATE A DATA BASE RESULTS SET
$rows = array
( 'BLINK10'     => 'RAY_temp_blink10_a.php'
, 'DAVEBALDWIN' => 'RAY_temp_blink10_b.php'
, 'RAY.PASEUR'  => 'RAY_temp_blink10_c.php'
)
;

// GET THE END OF THE URL AND NORMALIZE TO UPPER CASE
$nom = end(explode(DIRECTORY_SEPARATOR, $_SERVER["PHP_SELF"]));
$nom = strtoupper($nom);
$nom = mysql_real_escape_string($nom);

// IF THE NAME IS IN THE DATA BASE RESULTS SET
if (!array_key_exists($nom, $rows))
{
    header("Location: /");
    exit;
}

// LOAD THE ASSOCIATED PAGE
if (!include_once($rows[$nom]))
{
    echo "I AM INCLUDING {$rows[$nom]} HERE";
}

Open in new window

0
arober11Commented:
Hi

You still haven't explained what you are trying to achieve:

If your the admin of a server that host shell accounts for users Apache offers per user web directories, see: http://httpd.apache.org/docs/2.0/howto/public_html.html

If your writing an application, with a self contained user base, then have a look at this artical e.g.

# If the requested resource is not a physical file, directory or symbolic link redirect to a default PHP script.
RewriteCond %{REQUEST_FILENAME}  !-f
RewriteCond %{REQUEST_FILENAME}  !-d
RewriteCond %{REQUEST_FILENAME}  !-s
RewriteRule (.*)  /userprofile.php?request=$1 [L]

OR:

ErrorDocument 404 /somefile.php
0
blink10Author Commented:
^ ray...i am trying to implement this script, however can seem to get the error handler script to be triggered.



My htacess file says:
ErrorDocument 404 /subuser.php

And for testing purposes I tried to print something in subuser.php

However, when I type things like billsprice.com/testusername

I just get my default error message (someone else installed)

Thoughts?

0
pelbooksCommented:
Is your .htaccess file stored in the root directory of billsprice.com ?
0
blink10Author Commented:
yes
0
pelbooksCommented:
The default error message that appears is about a 404 error?

There are other errors you can trap as well:
http://www.angelfire.com/dc/html-webmaster/article12.htm

ErrorDocument 404 /subuser.php
ErrorDocument 403 /subuser.php
ErrorDocument 401 /subuser.php
0
blink10Author Commented:
The default message appears for all 400 messages, although i dont know where this was set, i need to change it i think, where do u think i should look?
0
pelbooksCommented:
The .htaccess file of the root directory has all the information you need. Have you got a mod_rewrite call in there which handles all page calls, including those not found? It may explain why the ErrorDocument is not triggered; it's probably because the rewrite handles the event and it does not register as "not found".
0
blink10Author Commented:
What is a mod_rewrite call and what does it look like?
0
pelbooksCommented:
RewriteEngine On

RewriteBase   /xyz

RewriteRule   ^oldstuff\.html$  newstuff.html


(http://httpd.apache.org/docs/current/mod/mod_rewrite.html)
0
blink10Author Commented:
Sorry for the delay on responding....

Anyhow this print anything:

$nom = end(explode(DIRECTORY_SEPARATOR, $_SERVER["PHP_SELF"]));
$nom = strtoupper($nom);
$nom = mysql_real_escape_string($nom);
echo $nom;

thoughts?
0
blink10Author Commented:
Nm, typo...now it just returns the name of php handler file...so no matter what you type

billsprice.com/anything

billsprice.com/somethingelse


you always get the name of the handler file, how do i get what the user typed?
0
idealwsCommented:
You could get the first line by using:

$pathinformation = parse_url($_SERVER["PHP_SELF"]);
$userinfo = explode('/', $pathinformation['path']);
 echo $userinfo[1];

Open in new window


Hope this helps.

Regards,
Ray
0
blink10Author Commented:
This still just names not the php file not what the user typed in.


billsprice.com/somethingRandom

generates an output of username.php

so is there anyway to get it to output somethingRandom instead of username.php ?
0
idealwsCommented:
You can add this to your .htaccess file:

DirectoryIndex index.php
RewriteEngine on
AddType application/x-shockwave-flash .swf
RewriteCond $1 !^(index\.php|images|css|js|swf|flash|mainvideo|robots\.txt|favicon\.ico|license.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

Open in new window


This is how I send the information to the index.php file and it works fine. I use it with Codeigniter framework but should work for your needs as well.

or use somethig like this:

I use this for a vehicle details script:
RewriteEngine on
RewriteRule ^(.*)/([\'\&a-zA-Z0-9_-]+).html$	username.php?username=$1 [L]

Open in new window


Url for this looks like so: www.example.com/USERNAME/SOmething-else.html

The above rule will get the USERNAME and post it to username.php?username=USERNAME

Then in your script you could write:
$username = $_GET['username'];

Open in new window


Hope this helps.

Regards,
Ray
0
blink10Author Commented:
with either example how do I get it to only work under case of a document not already existing (hence the need for a 404 or in my case to run username.php in my case)?
0
idealwsCommented:
There are many examples here that will redirect if a document does not exist. I was answering your original post on how to create a url with a persons username.

If you mean you need to test if the username actually exists then send them to one place and if it does not send it to another place then you can use the second one and in your username.php file do a check for the username against the DB then redirect if that username does not exist.

$username = $_GET['username'];
// Do some DB checking here
// If the DB returns no results test for it
// I am assuming you have connected to the DB already and selected it
$usernameexists = mysql_query("SELECT username FROM usertable WHERE username='$username'");
if(mysql_num_rows($usernameexists) >= 1){
   // They exists now show the page you want them to see
   echo "This user exists";
}else{
   http_redirect("404.php", array("username" => ""), true, HTTP_REDIRECT_PERM);
}

Open in new window


You can read more on redirect here: http://us2.php.net/manual/en/function.http-redirect.php

Hope this helps.

Regards,
Ray
0
idealwsCommented:
I should also mention that I have not put any security measures in place wiht the above code. You should still clean the $_GET['username'] before querying the DB to avoid any SQL injections. In other words never trust user inputted data.

Regards,
Ray
0
blink10Author Commented:
I am still confused....

I am golden when I can get the username variable in php.

Here is my problem. My current .htaccess file is:

ErrorDocument 404 /username.php
RewriteEngine on
RewriteRule ^(.*)/([\'\&a-zA-Z0-9_-]+).html$      username.php?username=$1 [L]


My username.php is:
<?php
$username = $_GET['username'];
echo $username;
?>

When I go to billsprice.com/somethingrandom

I get "Oops link is broken" from my browser.

What am I doing wrong?
0
idealwsCommented:
Remove the 404 error code: ErrorDocument 404 /username.php you can check for the user to exist in your PHP app. Unless you are specifically writing a file for every user such as thisuser.php and thatuser.php it will always be a 404 error.

As I stated in the last reply I would get the username into PHP then test for it in a DB or wherever your storing them. If it exists show the page you want to show if it does not then show an error.

Hope this helps.

Regards,
Ray
0
blink10Author Commented:
So if someone enters:

billsprice.com/somethingRandom

how will username.php get triggered if I dont have a 404 redirect?
0
idealwsCommented:
My .htaccess file:

RewriteEngine on
RewriteRule ^(.*)/([\'\&a-zA-Z0-9_-]+).html$      username.php?username=$1 [L]

Open in new window


My username.php file:

<?php
$username = $_GET['username'];
if($username == "username"){
	echo $username;
}else{
	echo "User does not exist";
}
?>

Open in new window


The URL: http://localhost/bob/this-user.html - User does not match so show User does not exist.

Other URL: http://localhost/username/this-user.html - User exists so echo the username

Hope this helps and gets you in the right direction.

Regards,
Ray
0
idealwsCommented:
You can also change the echo "User does not exist"; to a redirect and send them to your 404 page.

Or just print out an error. Send them to the register page. Just about anything you want from within your PHP file.

Regards,
Ray

0
blink10Author Commented:
So

RewriteEngine on
RewriteRule ^(.*)/([\'\&a-zA-Z0-9_-]+).html$      username.php?username=$1 [L]


will send every 404 to username.php first?
0
blink10Author Commented:
I deleted the 404 on my file...now no matter what I write after my url I get apaches default 404

you can try for yourself:

http://www.billsprice.com/randomWords
0
idealwsCommented:
You can use this as well if you do not want to have the .html after the username:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./username.php?username=$1 [L,QSA]

Open in new window


Let me ask this are you checking if the user exists before showing a 404? If so how are you testing it? Why would you send the user if they exist to a 404 page?

The above code will take every example.com/username and parse it through username.php with the variable $username in the get.

You can test these users using the following in username.php:
<?php
$username = $_GET['username'];
if($username == "username"){
	echo $username;
}else{
	echo "User does not exist";
}
?>

Open in new window


I would suggest checking the user against the DB first. You will never have a 404 if your calling username.php as this file is always there. You need to check for the users in your PHP code not .htaccess code.

Hope this helps clear it up a little bit for you.

Regards,
Ray
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
blink10Author Commented:
Thanks! I didnt realize you could rewrite like that...I was caught up on the .htaccess file part but it works perfect now.
0
idealwsCommented:
Your welcome, glad I could be of some help.

Regards,
Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.