Forum Moderators: phranque

Message Too Old, No Replies

preg match use in rewriteRule

         

mahipune

8:35 pm on Mar 23, 2010 (gmt 0)

10+ Year Member



Hi,

could someone guide me on one of the scenario which i have while writing RewriteRule for the below pattern?

Before:
/a/b/123456.html
/a/b/123456abc.html
/a/b/123456123.html
After (should be):
/a/b/12345x.html
/a/b/12345xabc.html
/a/b/12345x123.html

here, character on a specific position (here it's 6th) need to be replaced with another character (lets say 'x') with the help of rewriteRule and rewriteCond directive.
Question 1: can we use preg_match in .htaccess? if yes, then how can we? any example will certainly help me to write down the rule/condition and resolve my scenario.
Question 2: if preg_match can't be used, how can we achieve the same?

I'm using the below method using logical OR, however, i don't want to use this one as the character on specific position can change and also can't handle related patterns.
RewriteRule ^a/b/(12345)6(|abc|123).html$ /a/b/$1x$2 [NC,R=301]

Please guide.

mahipune

8:41 pm on Mar 23, 2010 (gmt 0)

10+ Year Member



Please send me notification incase of any reply.

g1smd

9:32 pm on Mar 23, 2010 (gmt 0)

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



Do you want to redirect requests for an old URL to a new URL, or you want to connect a URL request to an internal filepath?

One needs a redirect, the other a rewrite. More information is needed.

The pattern
([0-9]{5})[0-9]([0-9]+)
finds the first five digits and all after the sixth digit, and loads them respectively into $1 and $2.

mahipune

9:43 pm on Mar 23, 2010 (gmt 0)

10+ Year Member



basically i was trying to write the rewrite rule for the old URL's and convert these to internal filepath such as:

/a/b/c/123456.html to /a/b/d.php?m=1234x6
/a/b/c/123456a.html to /a/b/d.php?m=1234x6a

so the last character of the digit to be changed as x in the querystring.

Thanks for quick response.

g1smd

10:07 pm on Mar 23, 2010 (gmt 0)

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



What is the old URL that users used to see and use to access the content?
What is the old internal server filepath where the content used to reside?

What is the new URL (if there is a change in the URLs that users see and use to access the content)?
What is the new internal server filepath (if the content has been moved to a new location inside the server)?

That's four questions. Anything less than four answers will be incomplete.

mahipune

11:00 pm on Mar 23, 2010 (gmt 0)

10+ Year Member



the suggested pattern worked perfect as per my requirement. one additional thing, if the pattern is like below irrespective to the no of digits in the string, and if need to extract the digit part except the last number using back-reference

before:
123456
123456a
123a
1234ab

after:
12345x
12345xa
12xa
123xab

I was trying the below one but not sure how to find out the total no digits-1 and store into the $1...need to remove the hard coding of no of digits...
([0-9]{5})([0-9]{1})([a-z]*).html

Thanks again for all your help.

mahipune

11:11 pm on Mar 23, 2010 (gmt 0)

10+ Year Member



What is the old URL that users used to see and use to access the content?
ans: [test.com...]

What is the old internal server filepath where the content used to reside?
ans: /a/b/123456.html

What is the new URL (if there is a change in the URLs that users see and use to access the content)?
ans: [test.com...]

What is the new internal server filepath (if the content has been moved to a new location inside the server)?
ans: /a/b/abc.php?qs=12345x

mahipune

11:35 pm on Mar 23, 2010 (gmt 0)

10+ Year Member



many thanks for all your guidance :-) now i am able to get the desired result (extraction of last character from the digit part) with the following pattern:

earlier: ([0-9]{5})([0-9]{1})([a-z]*).html
now: (.*)(\d+)([a-z]*).html

g1smd

12:55 am on Mar 24, 2010 (gmt 0)

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



No. Do NOT use (.*) here. It is greedy and ambiguous, and very inefficient requiring many trial matches before finding the right one.

Use a more specific pattern.

As for the URLs I cannot understand why you now want to use URLs with parameters. By using a rewrite, you could maintain URLs that look like folders, while still having an internal script that works on parameters.

mahipune

5:18 am on Mar 25, 2010 (gmt 0)

10+ Year Member



Thanks for this valuable suggestion. You're absolutely right that (.*) could really affect the performance. Now, I'm using specific pattern as per my URLs, later can modify if more pattern will come. now it's somehow like this:

now: (12[0-9]*)(\d+)([abc]*).html

I did learn various new things over here in terms of rewrites and willing to contribute in case if i will find any query where i can jump into/resolve :-)

About the question asked, it's good suggestion to hide the querystring/parameters from users when we have the power of redirect and can organize via some folder structure. I will surely discuss with my team about this.

At the end, I'm trying one simple redirect but it's not escaping HASH character.

before: /abc
after: /a/b/c.html?key=value#anchor

Below are the tries but none of these is working. Any addition in flags? i tried with NE as well

RewriteRule ^abc/?$ /a/b/c.html?key=value\#anchor [NC,R=301] (escaping the hash)
RewriteRule ^abc/?$ /a/b/c.html?key=value%23anchor [NC,R=301]
RewriteRule ^abc/?$ /a/b/c.html?key=value%2523anchor [NC,R=301] (even using percent encoding for %)

g1smd

7:43 am on Mar 25, 2010 (gmt 0)

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






.. ..

jdMorgan

2:17 pm on Mar 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The "%" character itself must be escaped, and you may still have to use the NoEscape flag.

Try:
 RewriteRule ^abc/?$ /a/b/c.html?key=value\%23anchor [NC,R=301] 

-or-
 RewriteRule ^abc/?$ /a/b/c.html?key=value\%23anchor [NC,NE,R=301] 


Jim

mahipune

5:18 am on Mar 29, 2010 (gmt 0)

10+ Year Member



before posting the query, earlier i was trying with first one but missing the NE flag....now the second one is working perfect after NE flag add.

Many thanks.