Jordy
Jordy

Reputation: 85

.htaccess rewrite space to dash(-)

I got a dynamic URL I want to rewrite. I want to rewrite the space into a dash to not get %20 in the URL. My attempt was to create two parts in the .htaccess and put a @ sign in the middle of it when its sending the paranmeters to my PHP file. then in the query I used:

str_replace('@', ' ', $_GET['location_name'])

I did test this by printing it to my screen and it was showing the expected result.

A working not rewrited example:

/shop/article-filter.php?location_name=Data Tilburg&products_choise=0

Needs to be: /shop/articles-Data-Tilburg.html

I tryed the following rewrite rule which is redirecting me to the right page but its not showing any result. What is wrong, in the rewriterule?

RewriteRule ^articles-([^/]*)-([^/]*)\.html$ /shop/article-filter.php?location_name=$1@$2&products_choice=0 [L]

Upvotes: 1

Views: 1185

Answers (1)

ivanhoe
ivanhoe

Reputation: 4753

Why don't you try sending the whole 'Data-Tilburg' string to php, and then just replace dashes with spaces?

RewriteRule ^shop/articles-(.+)\.html$ /shop/article-filter.php?location_name=$1&products_choice=0 [L]

and then in php:

str_replace('-', ' ', $_GET['location_name']);

Presuming of course that original names don't contain dashes (your solution wouldn't work with that either).

Also be careful with that location_name string as if it contains multibyte chars str_replace() can screw them completely.

Upvotes: 1

Related Questions