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

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

Apache Web Server Forum

I got an error about replace a space with hyphen for mod rewrite

Msg#: 4546446 posted 2:25 pm on Feb 18, 2013 (gmt 0)


I tried to create a mod rewrite to list all cities in more friendly urls. Everything worked fine, except the space between two-word city show "%20". When I used Replace on php code to show a hyphen between two-words cities, then no results show up.

Here is my htaccess code:
RewriteRule ^(city|state)/([^/]*)-online-dating\/$ /search_results.php?$1=$2&mark=1 [QSA,L]
RewriteRule ^(city|state)/([^/]*)-online-dating\/pg\/([0-9]+)\/$ /search_results.php?$1=$2&pg=$3&mark=1 [QSA,L]

RewriteRule ^([^/]*)\/([^/]*)\/([^/]*)\/([0-9]+)\_([^/]*)\/$ /view_profile.php?id=$4 [QSA,L]
RewriteRule ^search_results\/([^/]*)\/([^/]*)\/$ /search_results.php?$1=$2 [QSA,L]
RewriteRule ^search_results\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/$ /search_results.php?$1=$2&$3=$4 [QSA,L]
RewriteRule ^search_results\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/$ /search_results.php?$1=$2&$3=$4&$5=$6 [QSA,L]
RewriteRule ^search_results\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/$ /search_results.php?$1=$2&$3=$4&$5=$6&$7=$8 [QSA,L]
RewriteRule ^search_results\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/([^/]*)\/$ /search_results.php?$1=$2&$3=$4&$5=$6&$7=$8&$9=$10 [QSA,L]

Here is my php code on search_results.php file:
echo "<a href='/city/".strtolower(str_replace(' ','-',$mem["sb_city"]))."-online-dating/' class='small_link'
title='All members from ".$mem["sb_city"]."'>".$mem["sb_city"]."</a>, ";

Can anyone please tell me what I need to change to get it works?




WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4546446 posted 2:41 pm on Feb 18, 2013 (gmt 0)

The slashes should not be escaped.

For readability add a blank line after each RewriteRule.

Your URLs look too complicated. Do you really need $1, $3, $5, $7, $9 to be in the URL? I think not.

far simpler than
and the "bogus" folder levels will end up causing a lot of problems.
I would remove those from the URL.


Msg#: 4546446 posted 5:39 pm on Feb 18, 2013 (gmt 0)

I don't know how to change them so can you help?
All I need to get is:
For example: domain.com/united-states/california/san-diego-online-dating.html



WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

Msg#: 4546446 posted 12:28 am on Feb 19, 2013 (gmt 0)


Since when does mod_rewrite support \d\d captures? I thought they cut out at nine :(

Picking one rule at random and getting rid of the unneeded escapes:

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([0-9]+)_([^/]*)/$ /view_profile.php?id=$4 [QSA,L]

What's the QSA for? Surely the point of those long URLs is that there is no query string; it's all in the body of the URL.

The formulation
is bad because it allows malformed requests with two or more consecutive // --and your code would interpret them as null directories, which I'm sure is not what you want. Always use

Now, if you're not reusing the first three groups, why capture them at all? In some cases you may need the parentheses for grouping; you can then mark them as (?:blahblah) for non-capturing. Here you don't need the parentheses at all:

^[^/]+/[^/]+/[^/]+/([0-9]+)_[^/]+/$ /view_profile.php?id=$1 [L]

But that leaves a bigger problem. The version with query string and the version without query string have to be deducible from each other within mod_rewrite. Any time you have a rule expressed as

RewriteRule aaa bbb [L]

there has to be a preceding rule in the form

RewriteRule bbb aaa [R=301,L]

If aaa and bbb aren't each deducible from the other, you're in php-script territory.

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