Welcome to WebmasterWorld Guest from 54.159.111.156

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

How do I replace spaces in URLs with hyphens

using Apache mod_rewrite

     
1:17 pm on Aug 23, 2005 (gmt 0)

5+ Year Member



Hi guys,

This is my first post.
I have a question for you.
if my url is [site...] , my .htaccess file translates into [site...]
But.. if instead of "product" i have "product name" my url will be [site...] and i want to be [site...] What should i add in my .htaccess file?

Options +FollowSymLinks
Options +MultiViews
RewriteEngine on
RewriteBase /

RewriteRule ^([^/]+)/(.+)\.html$ /$1?nume=$2 [L]

2:47 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



cristianlupu,

Welcome to WebmasterWorld!

Unfortunately with mod_rewrite, you cannot do a substitution, ie replace all spaces with dashes, but what you can do is create a new rule to match the space:

RewriteRule ^([^/]+)/([^\ .]+)\ ([^\ .]+)\.html$ /$1.php?name=$2-$3 [L]
RewriteRule ^([^/]+)/([^\ .]+)\.html$ /$1.php?name=$2 [L]

So basically you have one rule to match when there is a space, and one to match when there is not. If you have any products with more than one space, you will need a seperate rule for every possible number of spaces.

Chad

2:52 pm on Aug 23, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



cristianlupu,

Welcome to WebmasterWorld!

The following code will change all spaces to hyphens. It uses the [N] flag, which causes mod_rewrite to restart if a space is replaced. So, the first rule and any other rules above it will be re-processed until no more spaces remain. For this reason, it can be very inefficient; These rules should be placed at the top of your mod_rewrite code in your .htaccess file, or as close to the top as possible without interfering with the other rules.


# Replace spaces with hyphens
RewriteRule ^([^\ ]*)\ (.*)$ $1-$2 [E=rspace:yes,N]
# Redirect to update URL in search engine listings and browsers
RewriteCond %{ENV:rspace} yes
RewriteRule (.*) http://%{HTTP_HOST}$1 [R=301,L]

This is a "fix" for a pre-existing problem; You should change your "file.php" and other scripts so that URLs on your pages are constructed with hyphens instead of spaces. The usual method in php is to use the preg_replace directive.

Jim

[edit] Corrected as noted in msg #5 below. [/edit]

[edited by: jdMorgan at 7:52 pm (utc) on Aug. 23, 2005]

7:40 pm on Aug 23, 2005 (gmt 0)

5+ Year Member



ChadSeo: unfortunately, i've tried with your idea, but i couldn't make anything..(it doesn't matth my space urls)
jdMorgan: i get this error in apache's error log; also , in my browser i get a 500 error.

[Tue Aug 23 22:33:20 2005] [alert] [client a.b.c.d] /path/to/.htaccess: RewriteRule: bad flag delimiters

my .htaccess is something like that:

Options +FollowSymLinks
Options +MultiViews
RewriteEngine on
RewriteBase /

# Replace spaces with hyphens
RewriteRule ^([^ ]*)\ (.*)$ $1-$2 [E=rspace:yes,N]
# Redirect to update URL in search engine listings and browsers
RewriteCond %{ENV:rspace} yes
RewriteRule (.*) [%{HTTP_HOST}$1...] [R=301,L]

RewriteRule ^file1\.php$ file2.php [R=301]
RewriteRule ^([^/]+)/(.+)\.html$ /$1?nume=$2 [L]

7:47 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



cristianlupu,

The error you are getting is due to a missing \. It's on the following line:

RewriteRule ^([^\ ]*)\ (.*)$ $1-$2 [E=rspace:yes,N]

The first \, within the square brackets [] is missing (fixed in the line above).

Chad

10:09 pm on Aug 23, 2005 (gmt 0)

5+ Year Member



Well, there is a little problem..

I've made the change you suggeste me and also i added a "/" to this line, after HTTP_HOST:
RewriteRule (.*) [%{HTTP_HOST}...] [R=301,L]

I works perfectly when i run
'http://site/name1 name2' -> it changes to 'http://site/name1-name2'.
But when i run:
'http://site/product/name1 name2' it doesn't. My remote ssh connection crashed in the same time, my webpage refused to load and my mysql-server crashed also with this error:
/usr/bin/mysqladmin: connect to server at 'localhost' failed.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month