Kunal
Kunal

Reputation: 1452

Rewrite URL containing spaces

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. Is this possible?

Upvotes: 1

Views: 1917

Answers (4)

limboy
limboy

Reputation: 4089

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

Upvotes: 0

jaywon
jaywon

Reputation: 8234

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

Upvotes: 3

Luis
Luis

Reputation: 313

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

Upvotes: 1

Dor
Dor

Reputation: 7484

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_encodeuricomponent.asp

Upvotes: 2

Related Questions