htacces rewrite url

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
bhanu
Forum Commoner
Posts: 46
Joined: Thu Nov 05, 2009 4:25 am

htacces rewrite url

Post by bhanu »

hi

i have url like ' http://mysite.com/product.php?productId=1 '

in my site i want to display url like ' http://mysite.com/product.php/product-name '

please help me.

thank you

regards
bhanu
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: htacces rewrite url

Post by superdezign »

.htaccess is limited in this sense. Most of this relationship will have to be handled by the code. The way that your url is formatted to have the rest of the url after the PHP file name removes the need for an .htaccess file.

What you want to do is get the URL, which you can get from $_SERVER['REQUEST_URI']. Then, remove the physical URL (http://domain.tld/file.php) portion from the URL, and operate on that to get the product name. Then, just retrieve your data from the database using the name instead of the ID. For nicely formatted URLs, you'll probably want to have another field in your database table for the URL-friendly name of a product, and use that to get the data.
bhanu
Forum Commoner
Posts: 46
Joined: Thu Nov 05, 2009 4:25 am

Re: htacces rewrite url

Post by bhanu »

it becomes more complex. page loading will take more time.
in my database product names are like 'product name'

at present i coded query string .present url is ' http://mysite.com/product.php?pid=2'

rewreite url like ' httlp://mysite.com/product.php/product-name '

product name is like ' i pode ' , replace spaces with ' - ' , i-pode.

url will be ' httlp://mysite.com/product.php/i-pode '

using htaccess i want to display url format.

please help me
thank you
regards
bhanu
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: htacces rewrite url

Post by superdezign »

Again, .htaccess will do nothing for you, unless you want to turn http://domain.tld/product.php/product-name into http://domain.tld/product.php?name=product-name.

Either way, you will not have access to the ID number, only the name. The only way to get the data this way is via PHP.
bhanu
Forum Commoner
Posts: 46
Joined: Thu Nov 05, 2009 4:25 am

Re: htacces rewrite url

Post by bhanu »

for security purpose im using mod rewrite in my site.
i have coded .htaccess rewrite rules.

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ view.php?productId=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ view.php?productId=$1

it rewrites url from http://domain.com/view.php?productId=12 to http://domain.com/i-pode

now i want to hide query string for all of the pages in my site.

please help me
thank you
regards
bhanu
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: htacces rewrite url

Post by superdezign »

And just how are you rewriting a URL using unavailable information?
bhanu
Forum Commoner
Posts: 46
Joined: Thu Nov 05, 2009 4:25 am

Re: htacces rewrite url

Post by bhanu »

i have two URLs that I would like to rewrite:

1.

/artist.php?name=x => /x
2.

/albums.php?title=y => /y

This is what I put in the .htaccess file:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ artist.php?name=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ artist.php?name=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ albums.php?name=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ albums.php?name=$1

How should I edit the file so that the albums.php's URL would be executed as well? (Only the first one is working.) Thanks for your help.
bhanu
Forum Commoner
Posts: 46
Joined: Thu Nov 05, 2009 4:25 am

Re: htacces rewrite url

Post by bhanu »

thanks for all above replies.

http://domain.com/view.php?productName=i-pode to http://domain.com/i-pode.html

i used rewrite rules

RewriteEngine On
RewriteRule ^([^/]*)\.html$ /view.php?productName=$1 [L]

now i want to rewrite another pages
for that i coded the following rule

RewriteRule ^([^/]*)\.html$ /test.php?productName=$1 [L]

but it wont works for second url
how to code for multiple pages?

help me....
thank you in advance

regards
bhanu
socalocmatt
Forum Newbie
Posts: 15
Joined: Tue Nov 03, 2009 12:45 pm

Re: htacces rewrite url

Post by socalocmatt »

This is because both rewrite rules are being read as the same in the beginning:
^([^/]*)\.html$ = ^([^/]*)\.html$
both are the same thing. So with:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /view.php?productName=$1 [L]
RewriteRule ^([^/]*)\.html$ /test.php?productName=$1 [L]

If the URL domain.com/i-pod.html is given, how is the server supposed to know which one of the rewrite rules to use? It can't because ^([^/]*)\.html$ is true to both of them. Htaccess will find the first rule in the list that is true and execute it. To test, you can switch the rewrite rules and only the test.php will work.

You need to tell the server to use "view" or "test" somehow. For this, you may want to use a subfolders in the rewrite rule that to help separate them:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*).html /$1.php?productName=$2 [L]

This would make:
domain.com/anything/blablabla.html
Send
domain.com/anything.php?productName=blablabla

If you would like to use a product name, you may also want to use a unique id (auto incremental number) with your database, in case you come across an item that has the same name. You can add another subfolder for this as well:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*).html$ /$1.php?productName=$3&productID=$2 [L]

So the URL would read:
domain.com/view/19329/i-pod.html
And would send
domain.com/view.php?productName=i-pod&productID=19329

Just be sure your link paths, and images are relative to root and not relative to the document.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: htacces rewrite url

Post by Apollo »

bhanu wrote:RewriteRule ^([^/]*)\.html$ /view.php?productName=$1 [L]
RewriteRule ^([^/]*)\.html$ /test.php?productName=$1 [L]
These two RewriteRules rewrite the exact same pattern to two different destinations. What do you expect?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: htacces rewrite url

Post by superdezign »

socalocmatt wrote:domain.com/view.php?productName=i-pod&productID=19329
If you are sending the ID, there's no need to validate the name. The only reason to have the name in the URL with the ID is for SEO manipulation.
socalocmatt
Forum Newbie
Posts: 15
Joined: Tue Nov 03, 2009 12:45 pm

Re: htacces rewrite url

Post by socalocmatt »

I agree but the OP seemed pretty dedicated to passing the product name. :-)
Post Reply