URL - Redirect Using .htaccess

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anoopkp06
Forum Newbie
Posts: 2
Joined: Wed Jul 01, 2009 6:01 am

URL - Redirect Using .htaccess

Post by anoopkp06 »

HI ,

In my project i am using a code like

<a href="profile.php?id=12"> Profile </a>

Once the user clicks on this "Profile" then it will be redirected to page called profile.php. And the URL will be

http://localhost/Talent/profile.php?id=12.

Now the requirement is , When the user clicks on "Profile" link , then it should go to the page

profile.php but the URL should be
http://localhost/Talent/profile.php/myname

myname is the correspond to id 12.

Please tell me , how will i do this...
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: URL - Redirect Using .htaccess

Post by BornForCode »

Replace with what you need

Code: Select all

 
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ products.php?id=$1
The following example will rewrite the product.php?id=5 to porduct-5.html i.e when a URL like http://localhost/product-5.html calls product.php?id=5 automatically.
 
anoopkp06
Forum Newbie
Posts: 2
Joined: Wed Jul 01, 2009 6:01 am

Re: URL - Redirect Using .htaccess

Post by anoopkp06 »

I tried with this ... But it is not working... :banghead:
SeaJones
Forum Commoner
Posts: 48
Joined: Tue Jun 30, 2009 5:40 pm

Re: URL - Redirect Using .htaccess

Post by SeaJones »

There are a few possible issues that could be causing this. Start eliminating them by running a phpinfo call, to get a load of info about your system.

Create a file called "phpinfo.php" (you can call it something else if you like) and put this code into it:

Code: Select all

 
<?php
 
phpinfo();
 
?>
 
chmod the file to 755 to make sure it's executable and then run it, a screen of info should appear.

Possibilities, one-by-one:
  • If you see nothing, or an error message, php isn't working properly, this is unlikely.
  • Check the file for the web server running on the server, if you see apache, this is fine, if you see anything else, you may need to use slightly different syntax for your rewrite
  • Check the file for the words 'mod_rewrite'. If they aren't present then your system doesn't have mod-rewrite installed anyway and this is your issue.
This isn't every possibility, but it's a few more likely ones, other than this, on some systems (especially misguided hosts with a server and no brain) the htaccess file has been disabled, and you may need to ask for it to be enabled or even change hosts if this is the case.

Where are you hosting, on what server and is it shared, cloud, or a server(including VPS).

Owen
SeaJones
Forum Commoner
Posts: 48
Joined: Tue Jun 30, 2009 5:40 pm

Re: URL - Redirect Using .htaccess

Post by SeaJones »

Actually, scrub a lot of that, I just realised that the id you want to redirect to is *different* to the numeric id you originally used.

I.e. You want to redirect /profile.php?id=12 to /profile.php/name. You need to code this into your php. Mod-rewrite can redirect /profile.php?id=12 to /profile.php/12 but it can't look up which name corresponds to the id 12.

You need to write a function to query your DB to match a profile to a name, and include code in the profile.php file to use the query string to query this.

Second time I've written something like this today, but be REALLY careful when passing things from a URL to a database query. The potential for cracking is vastly increased.

Owen
Post Reply