homepage Welcome to WebmasterWorld Guest from 54.167.244.71
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
How do I replace spaces in URLs with hyphens
using Apache mod_rewrite
cristianlupu




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

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]

 

ChadSEO




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

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

jdMorgan




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

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]

cristianlupu




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

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]

ChadSEO




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

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

cristianlupu




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved