Page 1 of 1
htacces rewrite url
Posted: Thu Nov 12, 2009 10:31 pm
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
Re: htacces rewrite url
Posted: Thu Nov 12, 2009 10:36 pm
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.
Re: htacces rewrite url
Posted: Thu Nov 12, 2009 10:59 pm
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
Re: htacces rewrite url
Posted: Thu Nov 12, 2009 11:03 pm
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.
Re: htacces rewrite url
Posted: Fri Nov 13, 2009 10:39 pm
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
Re: htacces rewrite url
Posted: Fri Nov 13, 2009 10:41 pm
by superdezign
And just how are you rewriting a URL using unavailable information?
Re: htacces rewrite url
Posted: Sat Nov 14, 2009 12:05 am
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.
Re: htacces rewrite url
Posted: Tue Nov 17, 2009 12:45 am
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
Re: htacces rewrite url
Posted: Tue Nov 17, 2009 2:37 am
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.
Re: htacces rewrite url
Posted: Tue Nov 17, 2009 2:53 am
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?
Re: htacces rewrite url
Posted: Tue Nov 17, 2009 7:30 am
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.
Re: htacces rewrite url
Posted: Wed Nov 18, 2009 11:32 am
by socalocmatt
I agree but the OP seemed pretty dedicated to passing the product name.
