Forum Moderators: phranque

Message Too Old, No Replies

How to write RewriteRule "/charity/(.+)/(.+)/sign-up"

         

wergeni

7:06 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



Hi

For the moment I use this RewriteRule
RewriteRule /charity/sign-up/(.+)/(.+) /charity.php?cID=$1&pID=$2

I would like to change the order of the URL to
RewriteRule /charity/(.+)/(.+)/sign-up/ /charity.php?cID=$1&pID=$2

... but that don't want to work.

Can anyone point me in the right direction?

regards
Hakan

g1smd

8:57 pm on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



URLs are defined by the links on your pages.

Have you got new format URLs in the links on your pages?

Do those new URLs also include a trailing slash?

If search engines have already indexed the old-format URLs, you will need to also set up a set of 301 redirects from the old format to the new format.

wergeni

9:22 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



>Have you got new format URLs in the links on your pages?
New format URL's?
Brand new pages, so nothing picked up from SE.

>Do those new URLs also include a trailing slash?
A, see you point, not inconsistent above. Not sure but I think they don't. Do I have to decide?

I was not clear in what I want to achieve.

The URL I want to use is
mydoman.com/charity/oxfam/london/sign-up
mydoman.com/charity/$1/$2/sign-up

There Oxfam and London is $1 resp $2 for /charity.php?cID=$1&pID=$2

I can only get it to work for
mydoman.com/charity/sign-up/oxfam/london
mydoman.com/charity/sign-up/$1/$2

Problem seems to be that $1 and $2 need to be in the end of the URL.

Did that make any sense?

g1smd

9:30 pm on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your rules require the request to have a trailing slash.

Your example URLs do not have a trailing slash.

They will never match.

wergeni

9:55 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



Thanks g1smd, mucho appreciated.

What a forward slash can do...

jdMorgan

10:04 pm on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can speed up processing of the rule significantly by using less-ambiguous subpatterns and anchoring the pattern:

RewriteRule ^/charity/([^/]+)/([^/]+)/sign-up$ /charity.php?cID=$1&pID=$2 [L]

Note: I removed the trailing slash from the pattern -- assuming that's what you want. I'm also assuming this code is going into a server config file, and not into an .htaccess file.

Jim

[edited by: jdMorgan at 10:05 pm (utc) on Aug. 14, 2008]

wergeni

11:25 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



Hi jdMorgan

Anchoring the pattern make sense.

The ending trailing slash make me very confused, let’s say we looking at maindomain.com/folder, it don’t matter if I have a trailing slash or not. The idea with URL’s like this is that the user can guess the URL and just type it in. To get an error for using or not using the ending slash feels unnecessary. My coding I can always control... (Okay, no user except me will never type in the URL, still...)

What is the norm?

Been looking in the documentation, again, most of it over my head...

Thanks for mention [L], make sense on most of my rules.

You write ([^/]+)
I write (.+)
Yours looks more impressive but do the same thing huh?
Don’t get why my is more ambiguous, sorry, me stupid!

Guess it’s easier to not question stuff and just accept, on this one I trust a stranger/you more then myself :)

Thanks
Hakan

jdMorgan

12:05 am on Aug 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> The idea with URL’s like this is that the user can guess the URL and just type it in. To get an error for using or not using the ending slash feels unnecessary.

For best results in search engine rankings, any resource (for example, any "page") should have one and only one URL that can be used to reach it. Otherwise, you get duplicate content at several URLs, which can lead to loss of rankings. See this recent thread [webmasterworld.com] for more information.

Regarding the specific "[^/]+" versus ambiguous ".+" pattern, see the regular-expressions tutorial cited in our Forum Charter. The "[^/]+" pattern is much more specific because it says, "stop matching as soon as you find a slash," whereas, the ".+" pattern says, "match any number of any characters."

The effect of this is that the matching engine will attempt to match the rest of the URL --all of it-- into the first ".+" pattern, leaving the rest of the pattern to starve. Therefore, the first matching attempt will fail. So the matching engine will 'back off' leaving one character at the end of the URL, and try again. This again will fail. Only by trying many matching attempts will the matching engine 'back off' enough characters to leave the correct number of characters to match the slash after the first ".+", all of the characters in the second ".+", and the trailing "sign-up". This will take dozens matching attempts.

By using the specific "stop matching when you find a slash" pattern, the URL can be matched to the pattern in a single left-to-right pass. Therefore, it will match dozens to thousands of times faster, depending on how many ambiguous sub-patterns were used, what positions they matched in the URL, and the length of the requested URL.

Pass . $1 value .......... $2 value ......... match 
1 "oxfam/london/sign-up" . "" ............... fail
2 "oxfam/london/sign-u" .. "p" .............. fail
3 "oxfam/london/sign-" ... "up" ............. fail
4 "oxfam/london/sign" .... "-up" ............ fail
5 "oxfam/london/sig" ..... "n-up" ........... fail
6 "oxfam/london/si" ...... "gn-up" .......... fail
7 "oxfam/london/s" ....... "ign-up" ......... fail
8 "oxfam/london/" ........ "sign-up" ........ fail
9 "oxfam/london" ......... "/sign-up" ....... fail
10 "oxfam/londo" ......... "n/sign-up" ...... fail
11 "oxfam/lond" .......... "on/sign-up"...... fail
12 "oxfam/lon" ........... "don/sign-up" .... fail
13 "oxfam/lo" ............ "ndon/sign-up" ... fail
14 "oxfam/l" ............. "ondon/sign-up" .. fail
15 "oxfam/" .............. "london/sign-up" . fail
16 "oxfam" ............... "london/sign-up" . $1 and slash matched, others still fail
17 "oxfam" ............... "london/sign-u" .. fail
18 "oxfam" ............... "london/sign-" ... fail
19 "oxfam" ............... "london/sign" .... fail
20 "oxfam" ............... "london/sig" ..... fail
21 "oxfam" ............... "london/si" ...... fail
22 "oxfam" ............... "london/s" ....... fail
23 "oxfam" ............... "london/" ........ fail
24 "oxfam" ............... "london" ......... match

Jim

wergeni

12:38 am on Aug 15, 2008 (gmt 0)

10+ Year Member



Coin dropped!

Jim, I'm very impressed, it's people like you that makes internet a great place.

Many thanks
Hakan