Welcome to WebmasterWorld Guest from 54.196.231.129

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Regex rewriterule help needed

     
10:13 am on Apr 21, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:May 1, 2004
posts: 100
votes: 0


Help. I'm having difficulty getting my head around regex expressions.

I need to rewrite some 'friendly' urls so that they can be directed to a php script that will handle the output. More specifically....

RewriteRule recommended23.htm accommodation1.php?SL=1&P=23 [L]

In this case, I need to store the 1-2 digit number in recommendedxx.htm and use it for the P= query parameter.

I tried the following expression but it doesn't work. Where am I going wrong please?

RewriteRule ^recommended([0-9]+).htm$ accommodation1.php?SL=1&P=$1 [L]

12:00 pm on Apr 21, 2009 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 19, 2006
posts:508
votes: 0


How did you decide it doesn't work. What message do you get from your server?

You can try this:

RewriteRule ^recommended([0-9]+)\.htm$ /accommodation1.php?SL=1&P=$1 [L]

1:35 pm on Apr 21, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:May 1, 2004
posts: 100
votes: 0


Hi Wildbest

The only error message was an error 404 page. However, your code works perfectly. Thanks.

Can you use the same variable-capture techique in a 301 redirect instruction? For example, in the following example I would like to capture the page number (99) and add it to the end of the new page. I tried a variation of your code line, but just got another 404.

redirect 301 /accommodation1/1/99/accommodation.htmhttp://www.mydomain.com/recommended99.htm

Thanks again, either way.

2:05 pm on Apr 21, 2009 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 19, 2006
posts:508
votes: 0


RedirectMatch 301 ^/?accommodation1/1/([0-9]+)/accommodation\.htm$ [mydomain.com...]
5:14 pm on Apr 21, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:May 1, 2004
posts:100
votes: 0


Brilliant - I can see where I misunderstood things now. Thanks a ton wildbest!
11:15 pm on Apr 21, 2009 (gmt 0)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 0


If you are already using RewriteRule for some rules, then you must not mix Redirect and RedirectMatch rules into the same configuration. Use RewriteRule for all of the rules.
4:14 am on Apr 22, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


It's not so much "must not" as "should not." Due to different module processing order based on different server configurations and versions, code using mixed mod_alias and mod_rewrite directives might work just fine today. But potential disaster is only a hosting provider change or a server upgrade away.

We've had some members here get burned by this problem; It's a major concern if you do not control your server configuration file and that means it concerns most Webmasters, who are on shared hosting and have no access to the config files.

For this reason, it really is better to use mod_rewrite for all if you use mod_rewrite for any. Consider it "future-proofing."

Jim

9:18 am on Apr 22, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:May 1, 2004
posts: 100
votes: 0


Ouch! Thanks guys, but no sooner do I think I'm getting somewhere when you throw this in the pot to confuse me further.

We've had a major upgrade and reorganisation of the site meaning a) lots of rewrites necessary to translate 'friendly' URLs to scripts, and b) permanent redirects of old URLS. So, following my changes in this topic, my htaccess file is now littered with numerous of the following five types of general command:

1) RewriteCond %{HTTP_USER_AGENT} ^(aesop_com_spiderman地lexibot地narchie地nonymouse地spseek地sterias地ttach) [NC,OR]

2) redirect 301 /restaurants01c.php [mydomain.com...]

3) RedirectMatch 301 ^/?accommodation1/1/([0-9]+)/recommended_accommodation\.htm$ [mydomain.com...]

4) RewriteRule ^recommended([0-9]+)\.htm$ /accommodation1.php?SL=1&P=$1 [L]

5) RewriteRule hotels/village-haven.htmaccommodation2a.php?P=8 [L]

So, my question is: which of these 5 lines should be changed and can someone please help with the correct syntax which I can study and apply to other lines of that category.

I really appreciate the help I'm getting, even if I am ending up with a headache. :)

7:51 pm on Apr 22, 2009 (gmt 0)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 0


All of those using Redirect or RedirectMatch should be changed so that they use RewriteRule instead.

Make sure that for those that are edited, that the new RewriteRule lines have [R=301,L] at the end of each one.

11:43 pm on Apr 22, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


RewriteRule equivalents:
 2) redirect 301 /restaurants01c.php http://www.example.com/restaurants01.php
RewriteRule ^resrestaurants01c\.php$ http://www.example.com/restaurants01.php [R=301,L]

 3) RedirectMatch 301 ^/?accommodation1/1/([0-9]+)/recommended_accommodation\.htm$ http://www.example.com/recommended$1.htm
RewroteRule ^accommodation1/1/([0-9]+)/recommended_accommodation\.htm$ http://www.example.com/recommended$1.htm [R=301,L]

Note the absence of leading slashes on the RewriteRule patterns when used in .htaccess. Also note that RewriteRule is very similar to RedirectMatch.

Be aware that when using the Redirect directive, it works like this:
Redirect /old_path<and more path info may be here in the requested URL-path> http://example.com/new_path<and anything after the requested and matched URL-path that wasn't mentioned in the path-prefix appears here>

That is, the Redirect directive uses prefix-matching, and anything in the requested URL that is beyond the matched prefix in the directive gets added onto the end of the new URL specified on the right side of the directive. This is a common reason that folks have trouble with the Redirect directive, and especially if they've never looked at the mod_alias documentation.

As an example, your first Redirect

 Redirect 301 /restaurants01c.php http://www.example.com/restaurants01.php

will happily redirect /restaurants01c.phpAnythingAtAll to http://www.example.com/restaurants01.phpAnythingAt All

Jim

10:37 am on Apr 23, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:May 1, 2004
posts:100
votes: 0


Thanks Guys - I was just worried about any changes in syntax between the two methods. I really appreciate your help.
11:22 am on Apr 23, 2009 (gmt 0)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 0


RewriteRule allows you to be very precise/selective in which URLs to affect.