hi, i am a php developer.we have our social- networking site.
i want to implement mod_rewrite functionality in my site.for this i am working very hard.all the time a serious problem encounters when i try to use mod_rewrite.
actually i don't want to show query string in the url. you know this query string contains dynamic driven variables.
for example-
there is a link which gives some community's page in some website named"abc.com"
the link is-"http://abc.com/community.php?q=132
so it will give you the page of the community for which the value "132" is assigned to the variable "q"...say the name of that community is"metallica lovers".
Now as per our requirement i wanna give this stuff in url-
http://abc.com/mettalica lovers...
i want this page would open the desired community's page,name of which is "metallica lovers"....i write in ".htaccess file"

RewriteEngine On
Options +FollowSymlinks
RewriteBase abc.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ community.php?q=$1

ok...so what this code will do is it will store the name "metallica lovers" in the variable "q".....which will give 500 error..cuz there's not any page like this...
so I read many tutorials...took help from many friends...
and got to know that with the help of rewrite map I can get this problem solved...now..what I want is to retrieve the name of community and the values of that stored in variable "q"....ok..i can get this with the help of php code...now the main problem is that i want some dynamic code written in php...that gives me dynamic results...for example..if some user makes new account or some new user adds up with the website and make new group so the result should be fetched up with the help of this program....and this result should be stored in some mapping file..that would map the name of community with the corresponding value of "q"...

my dear friend can you help me out .........

Member Avatar for langsor

Maybe ...

You're storing all of your .htaccess (mod_rewrite) mapping in a mapping file? We will have to write to this file with PHP.

Show me the mapping file so I can see its exact format ...

Here (http://abc.com/community.php?q=132) I assume that 132 is a unique record-key in your database ... if you have every name of your sites be unique Metalica Lovers vs Metalica Lover then you could use those fields as unique keys in your database and use mod_rewrite to convert the URL from http://abc.com/community.php?q=Metalica%20Lovers to http://abc.com/community/Metalica_Lovers/ and parse that easily enough with mod_rewrite and PHP ... just a thought

Show me your mapping file and we can go from there ... PHP writes to files easily enough.

Maybe ...

You're storing all of your .htaccess (mod_rewrite) mapping in a mapping file? We will have to write to this file with PHP.

Show me the mapping file so I can see its exact format ...

Here (http://abc.com/community.php?q=132) I assume that 132 is a unique record-key in your database ... if you have every name of your sites be unique Metalica Lovers vs Metalica Lover then you could use those fields as unique keys in your database and use mod_rewrite to convert the URL from http://abc.com/community.php?q=Metalica%20Lovers to http://abc.com/community/Metalica_Lovers/ and parse that easily enough with mod_rewrite and PHP ... just a thought

Show me your mapping file and we can go from there ... PHP writes to files easily enough.

=========================================================
you are right dear...In my website...there's a unique id for each name.nd that id value is stored in variable"q".well...am not giving mapping file in .htaccess.In .htaccess file I am giving only RewriteRule nd RewriteCondition as you can see below.

<-------------------this is my .htaccess file..----------------->
RewriteEngine On
Options +FollowSymlinks
RewriteBase /www.music.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ group.php?q=${links:$1} [NC]

see the last line in .htaccess file.Here "links" is the name of the map.I have used php+mysql to extract the names and values of unique id from the database.AND in httpd.conf file I have given the path of that file.Below I am giving what I added in httpd.conf..

<------------------at the last of the apache config file I added thes lines--->
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteMap links "prg:C:/WINDOWS/Prefetch/php.exe d:/localhost/extract/retrieval.php"
</IfModule>

here "links" is the name of my rewrite map....
prg:C:/WINDOWS/Prefetch/php.exe->in this line "prg:" is the type of file...since it's external program so I hv given "prg:".....after prg:-I have given the path of php enterpreter.
d:/localhost/extract/retrieval.php->is the path where this php program is saved.

it's not working dude....I think there's some problem in the php program..that I made to retrieve data from database....actually it gives data..but how should i give relation between the "name" and it's "id"
so that if name is given in url...it can be replaced with id...

for example-
name q(it's a variable which stores corresponding id's)
metallica 132
guns n roses 133
judas-priest 134
rammstein 135
queens 136

now i give....in url-
www.music.com/metallica

it should give the page which corresponds to-
www.music.com?q=132(without showing this dynamic url)..

please give me some code snippet of php...for this mapping...
thanking you sir!

you are right dear...In my website...there's a unique id for each name.nd that id value is stored in variable"q".well...am not giving mapping file in .htaccess.In .htaccess file I am giving only RewriteRule nd RewriteCondition as you can see below.

<-------------------this is my .htaccess file..----------------->
RewriteEngine On
Options +FollowSymlinks
RewriteBase /www.music.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ group.php?q=${links:$1} [NC]

see the last line in .htaccess file.Here "links" is the name of the map.I have used php+mysql to extract the names and values of unique id from the database.AND in httpd.conf file I have given the path of that file.Below I am giving what I added in httpd.conf..

<------------------at the last of the apache config file I added thes lines--->
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteMap links "prg:C:/WINDOWS/Prefetch/php.exe d:/localhost/extract/retrieval.php"
</IfModule>

here "links" is the name of my rewrite map....
prg:C:/WINDOWS/Prefetch/php.exe->in this line "prg:" is the type of file...since it's external program so I hv given "prg:".....after prg:-I have given the path of php enterpreter.
d:/localhost/extract/retrieval.php->is the path where this php program is saved.

it's not working dude....I think there's some problem in the php program..that I made to retrieve data from database....actually it gives data..but how should i give relation between the "name" and it's "id"
so that if name is given in url...it can be replaced with id...

for example-
name q(it's a variable which stores corresponding id's)
metallica 132
guns n roses 133
judas-priest 134
rammstein 135
queens 136

now i give....in url-
www.music.com/metallica

it should give the page which corresponds to-
www.music.com?q=132(without showing this dynamic url)..

please give me some code snippet of php...for this mapping...
thanking you sir!

Member Avatar for langsor

Come to think of it, in order for this to work ...

metallica 132
guns n roses 133
judas-priest 134
rammstein 135
queens 136

www.music.com/metallica

Each name-string eg metallica must be a unique name (no-duplicates) or else you would get name-space collisions.

So armed with this insight you might be able to minimize the use of .htaccess for the redirect.

You could have one database table for lookup

<lookup>
----------------------
| NAME         | ID  |
----------------------
| metallica    | 132 |
----------------------
| guns n roses | 133 |
----------------------
| judas-priest | 134 |
----------------------
| rammstein    | 135 |
----------------------
| queens       | 136 |
----------------------

and one database table for data

<data>
------------------
| ID  | DATA ... |
------------------
| 132 | etc ...  |
------------------
| 133 | etc ...  |
------------------
etc ...

Here you can grab the lookup name and turn it into a query -- something like this

RewriteRule music.com/([a-zA-Z0-9-]+)/?$ music.com/community.php?q=$1 [L]

Where you simply look it up in your lookup table and retrieve the data-id field.

*deep breath*

Now you grab the data from the data table ... and either redirect the browser with PHP if that page already exists, or create it dynamically and redirect to it, or publish it directly to the page with Ajax or similar.

header('Location: http://www.music.com/132.php');

http://us3.php.net/manual/en/function.header.php

... or ...

print $data;

I hope something here might be helpful to what you're trying to do :-)

Member Avatar for langsor

Okay, I spent this morning building a bare-bones working version of my suggestions from my previous post ... see attached communities.zip file for all files in this example.

It seems to be working fine in my local environment, but that doesn't mean it can't be improved on or that there aren't any bugs in it -- it's just a starting place.

The primary functionality are the .htaccess and the communities.php files, which I'll post below.

Note on the .htaccess RewriteRule is that it will not allow any URL paths get past its doors, so place it in a sub-directory dedicated to this part of your site. I named my sub-directory communities -- thus the name of the zip file.

.htaccess

RewriteEngine On
RewriteBase /
RewriteRule ^(.*?)/?$ communities.php?q=$1 [QSA,L]

The core php code is as follows -- there is more in the zip file

communities.php

<?php
/*** NOTES AND STUFF ***
1. Make sure the .htaccess file is doing its job, then ...
2. You might want to disable the .htaccess file in order to do most of your other testing
3. Make sure your database login is working correctly

***/

// user defined variables
$index = 'index.html'; // default page

// keep this login stuff safe out of the public_html folders
$private_path = '/home/pixieportal/access'; // no trailing slash (/)
$private_path = 'access'; // local computer login info path for dev
require_once "$private_path/dblogin.php";
$conn = mysql_connect( $host, $user, $pass );
mysql_select_db( 'communities' );

// populate and test the new database here ... [OMITTED FROM SAMPLE CODE]
//$errors = populate_db( $conn, return_sql() );
//print_r( $errors );
//expose_db( $conn, 'communities' );

$query = $_GET['q'];
if ( empty( $query ) ) {
  if ( file_exists( $index ) ) {
    print file_get_contents( $index );
  } else {
    print '<h1>404 File Not Found</h1>';
  }
  exit;
}

// perform $query validation and transforms here ...
$comm_name = strtolower( rawurldecode( $query ) );
$result = mysql_query( "SELECT `comm_id` FROM `mapping` WHERE comm_name='$comm_name'" );
if ( mysql_num_rows( $result ) ) {
  $assoc = mysql_fetch_assoc( $result );
  $comm_id = $assoc['comm_id'];
  unset( $result );
  $result = mysql_query( "SELECT `comm_data` FROM `community` WHERE comm_id='$comm_id'" );
  if ( mysql_num_rows( $result ) ) {
    $obj = mysql_fetch_object( $result );
    // handle the data here with Ajax or by other means ...
    print $obj->comm_data;
  } else {
    die( 'No results for community data' );
  }
} else {
  // you might redirect to a search or index page if not found ...
  // print file_get_contents( 'search.php' );
  print '<h1>404 File Not Found</h1>';
}
exit;
?>

I hope this sample code is useful in some way.
Post back if you have any questions

...

Hey man!!
Thanks for giving me the solution of my problem.That's really great.
It will work.But my brother still I have some problem with it.You know my website has approx 155 files and it's already made(I have not made it..cuz I am a new employee).If the developers of my company had applied this logic at the time when they were making website then it would have been good.But now since it has been made,I ll have to make changes in various files.And it would be time consuming task.So If there's some simple way available to rewrite dynamic query string into static one then please let me know.I know Sir you can do it.

Thanks
Ashish

Member Avatar for langsor

Okay, refresh my memory ...

Can you give me an example of the dynamic query string and the static one you would like it turned into ...

I'm about to go to bed but can give it a couple minutes tongith.

ok...
my site name is-www.tagg.in(check it out sometime).
that's what appears now-
http://localhost/tagg.in/taggtivity.php?q=119
I wanna change it into-
http://localhost/tagg.in/hangout

in a table in database id and name columns are given like
grp_id-129 grp_name-hangout.

in my website grp_id is taken in query string.In your previous example you suggested me to give group name in query string-
$index = 'index.php'; // default page

// keep this login stuff safe out of the public_html folders
//$private_path = '/home/pixieportal/access'; // no trailing slash (/)
//$private_path = 'access'; // local computer login info path for dev
//require_once "$private_path/dblogin.php";
require_once('dblogin.php');
$conn = mysql_connect( $host, $user, $pass );
mysql_select_db( 'communities' );

// populate and test the new database here ...
//$errors = populate_db( $conn, return_sql() );
//print_r( $errors );
//expose_db( $conn, 'communities' );

$query = $_GET;
if ( empty( $query ) ) {
if ( file_exists( $index ) ) {
print file_get_contents( $index );
} else {
print '<h1>404 File Not Found</h1>';
}
exit;
}

// perform $query validation and transforms here ...
$comm_name = strtolower( rawurldecode( $query ) );

well this is the solution but for this the whole format of site I have to change.God it will be tough task.My manager is not allowing me to change the existing files..And he wants a solution too.He has assigned this task to me.

How about getting data dynamically from database by using rewrite map.and then give a key which will map grp_id with grp_name and then change the url by giving corresponding grp_name in place of grp_id.

Member Avatar for langsor

I have never used rewrite mapping before ... like you were trying to do in your first post.

You need to map http://localhost/tagg.in/taggtivity.php?q=119 to http://localhost/tagg.in/hangout in the browser location bar, or just in the PHP code? Or map the first to the second above in the location bar and back again in the PHP code?

You are obviously getting links coming in that look like the first taggtivity.php?q=119 but you don't want visitors to see that in the location bar?

Usually when you set up "friendly" URLs tagg.in/hangout you change them in the links pointing to the search page ... and then rewrite them for the server with mod-rewrite ... It looks like your id/name table would work to get the id if you had the name in the URL.

I will try to give this some thought tomorrow when I'm clear headed.

But please clarify exactly the situation about where you need the mapping done.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.