Link to home
Start Free TrialLog in
Avatar of blink10
blink10

asked on

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?
Avatar of arober11
arober11
Flag of United Kingdom of Great Britain and Northern Ireland image

What are you trying to do?
Avatar of blink10
blink10

ASKER

making everyone a profile page with a custom url...like how you can do on facebook
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.
Avatar of blink10

ASKER

ok so where do I learn how to make this 404 translator page?
I'll try to give you an example.  It may take a few monutes...
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

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
Avatar of blink10

ASKER

^ 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?

Is your .htaccess file stored in the root directory of billsprice.com ?
Avatar of blink10

ASKER

yes
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
Avatar of blink10

ASKER

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?
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".
Avatar of blink10

ASKER

What is a mod_rewrite call and what does it look like?
RewriteEngine On

RewriteBase   /xyz

RewriteRule   ^oldstuff\.html$  newstuff.html


(http://httpd.apache.org/docs/current/mod/mod_rewrite.html)
Avatar of blink10

ASKER

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?
Avatar of blink10

ASKER

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?
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
Avatar of blink10

ASKER

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 ?
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
Avatar of blink10

ASKER

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)?
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
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
Avatar of blink10

ASKER

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?
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
Avatar of blink10

ASKER

So if someone enters:

billsprice.com/somethingRandom

how will username.php get triggered if I dont have a 404 redirect?
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
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

Avatar of blink10

ASKER

So

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


will send every 404 to username.php first?
Avatar of blink10

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of idealws
idealws
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of blink10

ASKER

Thanks! I didnt realize you could rewrite like that...I was caught up on the .htaccess file part but it works perfect now.
Your welcome, glad I could be of some help.

Regards,
Ray