English 中文(简体)
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?

最佳回答

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




相关问题
Using SimplePie with CodeIgniter and XAMPP

I am using CodeIgniter 1.7.2 with XAMPP 1.7.2 on a Windows computer. I am trying to make use of SimplePie. I followed all the instructions I could find: a copy of simplepie.inc is in my applications/...

Multiple Sites with common files

I have developed over 50 sites that all use the exact same files other than CSS and IMAGES, I currently duplicate the files each time I create a new site and upload different css and images. What ...

http server validation

I finish a litle http server, writing from scratch. I would like to be sure that my imlementation is conforme to the HTTP specifications. W3C give us tools for HTML/XML conformance, but i see nothing ...

热门标签