URL Rewrite

Hello Friends Please help me !!

I want to change this url (http://localhost/shop/product.html?sub=Table-Clocks)
this URL (http://localhost/shop/product/Table-Clocks)

For This Page only and Using htaccess file.

you’ll have to use mod-rewrite as you can’t pass the variable part in a simple redirect.

is your page actually .php not .html in the first link?

If so you could code it similar to how i do it… in your php page you need

//use the variables in the URL string for the query
$data = explode("/",$_SERVER['PATH_INFO']);
$_GET['section_one'] = $data[1];
$_GET['section_two'] = $data[2];
$_GET['title'] = $data[3];

this will allow you to pull out the variables from a url (in this example there are 3 variables but could be 1 or however many you want)

so example.php?section_one=this&section_two=that&title=other

becomes example.php/this/that/other


obviously this leaves the .php part so i use .htaccess to remove the .php part

<IfModule mod_rewrite.c>
RewriteEngine on

# If a script is called without .php extension, but with /variables
RewriteCond %{REQUEST_FILENAME}.php -f
#RewriteRule ^([a-zA-Z0-9\\._-]*)?/(.*)$ $1.php [QSA,E=PATH_INFO:/$2,L]
RewriteRule ^([a-zA-Z0-9\\._-]*)?/(.*)$ $1.php/$2 [QSA,E=PATH_INFO:/$2,L]

# If a script is called without .php extension, and without /variables
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]

# Fix PHP Authentication with FastCGI mode
RewriteCond %{HTTP:Authorization} !''
RewriteRule .*php - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]


Needs FastCGI though so not sure if you have that on your server. it’s pretty simple to check as you can just upload that as .htaccess and try a file without .php on the end and if it works its should just load the page.

If that’s all too much for what you need you could have a look here http://kb.mediatemple.net/questions/242/How+do+I+redirect+my+site+using+a+.htaccess+file%3F#all/-more-powerful-url-changes-with-modrewrite--


Thank You for helping me but i am facing one problem with this code

My URL become http://localhost/ebay/product.php/product/product/Table-Clocks when i click on product.php page more then 1 time please help me.


The problem with what you’ve described is that the server doesn’t know what to do with http://localhost/shop/product/Table-Clocks (because it’s not a file which can be served).

Okay, if you’re using a CMS that can accept this format and convert it into something usable (like http://localhost/shop/product.html?sub=Table-Clocks) then product.html should be able to provide the code for a webpage (Really? An html file accepting a query string?).

It looks like Noppy got carried away with his mod_rewrite code as the answer to your question (as stated) should be very simple. However, rather than simply providing code, have a read of the tutorial article linked in my signature as it’s helped SitePoint members for nearly 10 years. THEN, come back with your attempt (or PM your attempt to me) and we (or I) will get you through to your answer. (That’s the polite way to say I don’t code for “script kiddies” but, from your description, you need to get your head around mod_rewrite’s purpose then coding and the tutorial … with many explained examples … should help.)



oops sorry if that was one step too far but it was the way it worked for me. I should have amended my code though as you only want one variable

//use the variables in the URL string for the query
$data = explode("/",$_SERVER['PATH_INFO']);
$_GET['sub'] = $data[1];


This should get you to be able to output the page like this


and then the .htaccess part will all you to call the page without the .php

so you get


the one server i use doesn’t have fastcgi though so i can’t get rid of the .php which is a pain.

Probably is a simpler way but this works for me :slight_smile:

Thank Sir i got solution from you thanks alot