views:

194

answers:

5

I have an url with the following format: domain/product.php?name=product a

Now I need to rewrite this to: domain/product a(including the space in the word) like: http://www.directline-holidays.co.uk/Costa Blanca

How do I do this? The closest result I got so far is the following: domain/p/product-a

With the following code in .htaccess

RewriteEngine On 
RewriteRule ^p/([^/]*)/$ destination.php?name=$1

I could not even use the name without the "-", I need the product name just as it is in the database. Please help. Many thanks in advance.

+2  A: 

add "%20" to the URL, such as:

http://www.directline-holidays.co.uk/Costa%20Blanca

20 in hexadecimal base is the ASCII number for space.

Edit:

In addition to powtac's comment:

Use the JS encodeURIComponent() function to encode a value that should be used in a URL: http://www.w3schools.com/jsref/jsref%5Fencodeuricomponent.asp

Dor
If you use PHP to create the links add urlencode() around the link, it will add the %20 and other stuff automatically.
powtac
thanks, i have solved it already. now what i need is the rewrite rule which will allow me to make the url only with the product name. pls help.
Kunal
@Kunal: Try the following REGEXP:^[^/]$
Dor
You cannot have an URL with a space: what you provided (`domain/product.php?name=product a`) is not a valid URL. Using the proper form (`domain/product.php?name=product%20a`) will give what you want.
ntd
@Dor ur soln worked, but when i tried:RewriteRule ^[^/]$ destination.php?id=$1and the url:http://localhost/zip2/testit said object not found!any idea?
Kunal
The requirement isn't clear, but try: RewriteRule ([^/])$ destination.php?id=$1
Dor
+1  A: 

This should work: It will simply give you the rest of the URL string after the /p/ directory to the end of the string, which in your case should be the end of the URL, correct?

RewriteRule ^p/(.*)$ destination.php?name=$1

For the pages that are not product pages, if you know they will end in a .php file extension you can filter for those pages with the following rule:

 RewriteCond %{REQUEST_URI} !^.*(destination\.php).*$ 
 RewriteRule ^([^\.php]+)$ destination.php?name=$1

EDIT: Fixed for infinite loop condition by adding RewriteCond for destination.php

jaywon
with the /p/product name format i already got it working, the issue is that i want only the product name after the domain name. without the /p
Kunal
i see...so should just be: RewriteRule ^(.*)$ destination.php?name=$1
jaywon
its redirecting everything to the destination.php page...need to have a check so that it does not redirect .php pages like domain/index.php, domain/contact-us.php etc pages to the destination pages...event only typing domain/ is getting redirected to the destination.php page...ny idea?
Kunal
i even tried RewriteRule ^([A-Z0-9]*)$ destination.php?name=$1...but again object not found issue :(
Kunal
@Kunal - check my edit for another suggestion on filtering for *.php files
jaywon
@jaywon - Man thanks a ton or your help. Cheers!
Kunal
+1  A: 

Try this rules..

Options +FollowSymLinks
RewriteEngine on
RewriteRule product-name-(.*)\.htm$ product.php?name=$1

or

Options +FollowSymLinks
RewriteEngine on
RewriteRule product/name/(.*)/ product.php?name=$1
RewriteRule product/name/(.*) product.php?name=$1
Luis
I prefer to use the first one I wrote.. I had problems with the second one depending the server I use.
Luis
A: 

do you mean if destination is a php file , doesn't rewirte it , else rewrite any thing to destination.php ? if so , this should work

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ destination.php?name=$1
lzyy
A: 

Here is the latest .htaccess file i have, with this i can now read pages like domin/destination name...which will take me to destination.php?id=destination name,

Options +FollowSymLinks
RewriteEngine On 
RewriteRule ^cheap-holidays$ holidays.php
RewriteRule ^destinations$ destinations.php
RewriteRule ^cheap-hotels$ hotels.php
RewriteRule ^booking-conditions/$ booking-conditions.php
RewriteRule ^zipholiday-news/$ news.php
RewriteRule ^zipholiday-faqs/$ faq.php
RewriteRule ^zipholiday-useful-links/$ links.php
RewriteRule ^customer-request/$ cs_request.php
RewriteRule ^contact/$ contact-us.php
RewriteRule ^page/([0-9]+)$ page.php?pageid=$1
RewriteRule ^news/([0-9]+)$ news.php?cat=$1
RewriteRule ^news/([0-9]+)/([0-9]+)$ news-details.php?cat=$1&newsid=$2
RewriteRule ^faq/([0-9]+)$ faq.php?cat=$1
RewriteRule ^faq/([0-9]+)/([0-9]+)$ faq-details.php?cat=$1&faqid=$2
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ destination.php?id=$1&tab=$2
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ destination.php?id=$1

now what i need to do is that if somebody types a page url like domain/abcd, which basically does not exist or any other format like domain/sdd/sdds/22323, then it should give a 404 err and redirect to a specific page. How can I achieve that? Please advice.

Kunal