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

Regex rewriterule help needed

 10:13 am on Apr 21, 2009 (gmt 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)

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)

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)

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


 5:14 pm on Apr 21, 2009 (gmt 0)

Brilliant - I can see where I misunderstood things now. Thanks a ton wildbest!


 11:15 pm on Apr 21, 2009 (gmt 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)

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."



 9:18 am on Apr 22, 2009 (gmt 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)

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)

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



 10:37 am on Apr 23, 2009 (gmt 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)

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

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