Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

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



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?



2:41 pm on Feb 18, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

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.


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



12:28 am on Feb 19, 2013 (gmt 0)

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


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.

Featured Threads

Hot Threads This Week

Hot Threads This Month