mod_rewrite php mysql - php

I'm really new at mod_rewrite and i have been trying to figure this out but really stuck. p
Here is my issue.
I have a page http://example.com/user/?s=81
?s=81 is reading from user id in the db.
What i would like to have is a link
http://example.com/nookie
In the database i have a field called whatuser
so on row 81 in that field i have user nookie
So what i would like is to read from databse what user it is in database
and create easi url from it.
i have also several php pages inside that user folder so i need to be able to link
to them like
example.com/nookie/step1.php
example.com/nookie/step2.php

To my knowledge you can not query databases with mod_rewrite.
how about putting a PHP-script to /user/?s=81, that looks up the user's name in the db and then relocates the user to $url = "/$username"; see PHP's header function passing "Location: $url" to it.

The other possibility (that I haven't thought through too much :) ) is that you add an extra field to the database e.g. user_home = /nookie/. So the logon script grabs it when verifying account details and thats where you send them to on successful validation? No rewrites /extra scripts needed.

Here's what I would suggest: if your username is only alphanumeric chars, you could pass the nick to all your Php files, after an internal rewrite, in the GET query.
Something like:
RewriteRule /([a-zA-Z0-9]+)$ /user/?s=$1 [QSA,L]
RewriteRule /([a-zA-Z0-9]+)/(.*)$ /$2?s=$1 [QSA,L]
And then, just have the "mother of mother" classes in Php that check for a value "s" in the query string ($_GET) which checks in the database if the user exists. If the user doesn't exists, make a 404 in Php.

I have almost sorted it out and here is the solution so far:
Here is the .htaccess row
RewriteRule ^([a-z0-9_-]+)$ user/?s=$1 [L,NC]
Then in the index file i have the following:
$trafikskola_id = mysql_real_escape_string($_GET['s']);
$vilken_trafikskola = mysql_query("SELECT * FROM users where slug='$trafikskola_id'") or exit(mysql_error());
In the database i have create slug field and inside that on row 81 added nookie and my url looks like
http://mypage.com/nookie instead of http://mypage.com/user/?s=81 =))
However i have the issue that i can not have link
http://mypage.com/nookie/ to have a forwardslash
And aswell non of my css - js - images are working.. Any ideas to solve those issues?

Related

How to set a custom Profile URL for my user?

you all know that if you login to your Facebook or Twitter account, you see a URL on address bar like this: https://web.facebook.com/iamakm15. That means your username appears on URL. My question is, how Facebook and Twitter did this? I mean what programming language did they use to show the username in the URL?
Now, when I login to my profile, I get a URL like this: http://localhost/AKM.PHP/project_akm/profile.php, so how can I add username after profile.php like this: /profile.php/imakm15 to my URL?
Later later edit ( didn't have space for full explanation in comment)
Ok, good. Let's start with the beginning. When you don't have rewrite setup on yoru server, your link will look like this:
http://localhost/AKM.PHP/project_akm/profile.php?username=iamakm15
Profile.php will extract the parameter with $_GET['username'] in your php code, it will make a query in database, where clause username='iamakm15' and it will extract the info associated with 'iamakm15'. If this step is already done and it works, in your root localhost ( you need to place this file at this level:localhost in order to work), add a file called '.htaccess' - if you already have one, just add these lines:
RewriteEngine On
RewriteBase /
RewriteRule ^AKM.PHP/Project_akm/([^/\.]+)/?$ /AKM.PHP/Project_akm/profile.php?username=$1 [L]
After saving this file, just try to access url:
http://localhost/AKM.PM/project_akm/iamakm15
and you should see the page with info about user: iamakm15. Make this exercise with different usernames from database, by modifying iamakm15 with other one.
if you get a 500 error you can add a new rule ( after RewriteBase / ):
RewriteRule ^/AKM.PHP/Project_akm/profile\.php$ - [L]
because can match itself after redirect.

Use apache mod rewrite with php variable inside page

I am displaying a record from a database by passing a GET parameter into a url: http://example.com/record?id=2
The record has a field named "title" that I would like to use to rewrite the url to display like this:
http://example.com/record/title
Example: http://example.com/record/This-is-the-Record-Title
Inside the page I have a variable $title that pulls the "title" value from the record. How can I use the $title variable to rewrite the variable like shown in the example above?
The answer is two-fold:
Set up mod_rewrite to split the request into variables (use search for mod_rewrite topics), and
Create reverse routing which takes This-is-the-Record-Title and translates it into number 2
Hope that takes you where you are trying to go.
Since the information about the title is not available to mod_rewrite, you will better set a redirect from PHP. Like:
/*** find out record title here ***/
$_SESSION['id'] = $_REQUEST['id'];
header('HTTP/1.1 302 Found');
header('Location: http://example.com/record/'.$record_title);
The id can be stored in a session variable, so you do not have to reverse-retrieve it. Still better would be to use a verified value, such as the id returned from the database row, because this is not prone to any modifications from user side. However, if users access the record via the "pretty link", you will have to lookup the record from the title.
For mod_rewrite, you should setup an internal rewrite, so the record title and not the actual script will be displayed to the users:
RewriteEngine on
RewriteRule "^/record/(.*)$" "/records.php?title=$1" [PT]

How can there be a user directory in the URI?

Apologies if this is a repeat, I really did not know what to search for this.
How can there be a user directory in the URI without creating a file for each user (which surely would be inefficient)? Or is that what is actually happening?
For example, youtube has "youtube.com/user/[username]" and it would take you to that specific profile. It seems similar to the GET request method (eg "?Profile=[username]"). Is this accomplished via PHP/.htaccess? If so then please tell me how to do via PHP/.htacess for my own use. If not, then I would still like to know the theory of how it is done.
Many thanks!
Jon
You can use apache mod_rewrite, i.e.:
Create an .htaccess file on the root of your website with the following content:
RewriteEngine on
RewriteRule ^user/(.*)$ /user.php?user=$1 [NC]
The above will redirect any request to http://yoursite.com/user/janedoe to user.php. To get the value on user.php, you can use:
<?php
if(!EMPTY($_GET['user'])){
$username = $_GET['user'];
//janedoe
}
?>
now, you can use the value of $username to, for example, query a database to get specific user details.

Creating dynamic file in php (not the title)

What I want to do:
Create dynamic php file for each product. What I'm currently doing is, if a person clicks on a products , it opens in one single file with some data i.e. www.example.com/showproduct.php?ref=1085. Here 1085 is a productid & thus it shows the content from DB for the particular id.
Is it possible to show the URL as www.example.com/google-nexus-5-16gb-black.php, where this data comes from DB content?
I don't want to create a page for each product, I think that would be a waste. Is there any solution? You can see even on stackoverflow how it shows the URL for each question.
EDIT
I've used following code in .htaccess file, however it's not rewriting the URL.
RewriteEngine on
RewriteRule ^ref/([A-Za-z0-9-]+)/?$ showproduct.php?ref=$1 [NC]
Any problem with this? And my I will try connecting to DB & get the file name once I know how this works.
You can create a unique alias field for each product record in your DB, and then you can use mod_rewrite to map www.example.com/YOUR-ALIAS to www.example.com/showproduct.php?alias=YOUR-ALIAS. This will give your the desired friendly URL effect.
Update
In your case it appears that you merely want to map google-nexus-5-16gb-black.php to showproduct.php?ref=1085, creating a additional alias field may be overkilled. In this case, you may setup a single rewrite rule:
RewriteEngine on
RewriteRule ^google-nexus-5-16gb-black.php$ /showproduct.php?ref=1085 [L]
.htaccess
URL rewriting file. Redirecting original URL www.example.com/showproduct.php?ref=1085 to www.example.com/google-nexus-5-16gb-black.php
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+).php$ showproduct.php?ref=$1
RewriteRule ^([a-zA-Z0-9-/]+).php/$ showproduct.php?ref=$1
You can read more about Basic URL Rewrite
<?php
include('db.php'); // Connection to your database
if($_GET['ref'])
{
$url=mysql_real_escape_string($_GET['ref']);
$sql=mysql_query("select title from table where ref='$url'");
$count=mysql_num_rows($sql);
$row=mysql_fetch_array($sql);
$title=$row['title'];
}
?>
Probably this link will help you to do what you are looking for

htaccess mod_rewrite

I'm trying to put something with this, whenever I go to a page like:
http://www.example.com/character.php?id=3
I want the mod rewrite to change it to:
http://www.example.com/character/Jim_Carrey
Which of course, the ID is the row of the character name...
For that kind of example... I've tried to work with it, but don't seem to get most of the production of htaccess because I haven't worked with .htaccess a lot really.
It's actually the other way around:
On your website, you write all your links the way you want them to look. For instance http://www.site.com/character/Jim_Carrey
In your database, you add a field,
commonly called "slug" or
"post_slug" which refers to the
Jim_Carrey" part of the url. IT MUST
BE A UNIQUE ELEMENT, much in the way
of a primary key. So make sure you
have a function that does take care
of creating the slug based on a
given string (the post title for
example) and making sure there is no
duplicate.
Then in your .htaccess (on the root folder) you do
something like
RewriteEngine On
RewriteRule ^character/([a-z0-9_\-]+)/$ character.php?slug=$1 [L,NC]
Finally, in your character.php
script, you do a database query not
against the ID, but against the
post_slug field.
If you're only using the character's name, then something like the following would do
RewriteRule ^character/(.*)$ /character.php?slug=$1
with a URL of eg http://www.example.com/character/Jim_Carrey. You'll then need to look up the character's name in the database using the slug passed in, as you won't have the ID to look it up with.
Alternatively you could include the ID in the URL if you need it, vis:
RewriteRule ^character/([0-9]+)/.*$ /character.php?id=$1
This way you could have a URL like http://www.example.com/character/3/Jim_Carrey which would include the character name (for SEO reasons etc etc), but also the ID which you could then look up directly in your database.
Edit a small PHP example for you re the first one:
<?php
// ... database connection stuff here etc
$slug = $_GET["slug"];
// IMPORTANT: perform some data sanitization here
// I'm just going to make sure it's only letters, numbers and
// hyphens/underscores as an example.
$slug = preg_replace("/[^a-zA-Z0-9\-_]+/", "", $slug);
// Now look up in your database
// Ideally you'd have a slug column to compare this with, that you can fill
// when your record is created/updated
// You'd also be best off using bound parameters here, rather than directly
// adding the data into the query string. I personally use the MDB2 PEAR
// module but feel free to use whatever you normally use.
$qry = "SELECT * FROM characters WHERE slug='" . $slug . "'";
$result = mysql_query($qry, $db);
// do something based on this result, fail if none found
// or show details if OK etc
// ...
?>
Hope this helps! As always, use bound parameters where possible for your queries, and perform sanitization of your user data well. The PEAR MDB2 module has a nice page on how to do this here.
Edit 2 a quick and dirty setup :-)
.htaccess file as follows:
RewriteEngine On
RewriteRule ^character/(.*)$ /character.php?slug=$1
Your .htaccess file would ideally be in the root of your site. Eg /home/wayne/public_html/ or wherever your index file is served from
A URL to match that would be http://www.example.com/character/Jim_Carrey - with the phrase "Jim_Carrey" appearing in your $_GET array as $_GET["slug"]. NB apologies, wrote that PHP sleepy last night above so no wonder $_POST wouldn't work as its a GET request :-) I've updated it now!
Finally you need to make sure that your host supports the use of .htaccess files. The setup of this is out of the scope of SO so any Apache configuration questions you'd be best asking over at https://serverfault.com/
I'm fairly certain you can't do this through htaccess. You'll need to do it in PHP, querying the database using the information from the url (?id=3) and then calling the Header Function using what you've learned from the database.
Sounds like you might be able to use mod_rewrite rewritemap.

Categories