homepage Welcome to WebmasterWorld Guest from 54.204.215.209
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
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
jennypretty




msg:4546448
 2:25 pm on Feb 18, 2013 (gmt 0)

Hello,

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?

Thanks.

 

g1smd




msg:4546449
 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.

example.com/34/5/2
far simpler than
example.com/chapter/34/page/5/section/2
and the "bogus" folder levels will end up causing a lot of problems.
I would remove those from the URL.

jennypretty




msg:4546515
 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:
domain.com/country/state/city-online-dating.html
For example: domain.com/united-states/california/san-diego-online-dating.html

Thanks.

lucy24




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

$9=$10

!
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
[^/]+/
instead.

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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved