0

Clean url in php using htaccess
Here is an example

http://www.example.com/Mobiles/index.php?idd=4

and i want result like this

http://www.example.com/Mobiles

Please help

2
  • You don't want to pass the idd=4 in the new url ? Commented Apr 2, 2015 at 9:58
  • yeah.. don't want to pass it and also i don't want to display index.php Commented Apr 2, 2015 at 10:15

1 Answer 1

1

This is called URL Rewrite. If your page links are dynamic, like extracting data from database which is mostly the case in e-commerce sites, then the best approach is to append the id at the end of URL. This way you can fetch the data from database. Like in your case, your new URL might look like:

http://www.example.com/Mobiles/4

When user will visit this link .htaccess file will internally rewrite this URL to:

http://www.example.com/Mobiles/index.php?id=4

In this way you can then retrieve id from your PHP like this:

$id = $_GET['id'];

or:

extract($_GET);

This extract function will create the variables automatically from the parameters name and you can access it directly with $id variable.

Here is the .htaccess code:

RewriteEngine On
RewriteRule ^Mobiles/(\d+)$ http://www.example.com/Mobiles/index.php?id=$1 

In case if you don't need URL like http://www.example.com/Mobiles/4, then use this:

RewriteEngine On
RewriteRule ^Mobiles$ http://www.example.com/Mobiles/index.php?id=4
Sign up to request clarification or add additional context in comments.

8 Comments

thnks for rply.. let me try it.. and can u please tell me if i have multiple links like this then i have to write the above code for multiple times ?
@RajeshKumar No, only one entry in .htaccess is all you need, if your URL is in this format http://www.example.com/Mobiles/index.php?id=4. If your id is different, then use the first example of .htaccess I posted that will accept any id.
thnks.. bt i mean to say if i have many categories like mobile , TV etc etc .. in that case what to do ? can u please tell me..
This is a different thing now. Your actually question is completely different from what you are asking now. In this case mostly the categories id's are added in parameters like cat-id and then it needs slight modification in .htaccess.
thnks for reply.. Actually i got ur answer that u have told me above to rewrite url.. bt i was asking if there are many categories .. so in that case can i generate url like this example.com/Mobiles/4 example.com/TV/2 example.com/AC/1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.