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 开发者_C百科this possible?
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
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
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
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
精彩评论