Forum Moderators: phranque

Message Too Old, No Replies

.htaccess recursive rule set

         

enghien

3:07 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



Hi,

I'm searching a single rule (when possible) enabling to deal with the 3 following cases :

1 rewrite : /xampp/www/news-summer/s0d0m0p0ak.html -> /xampp/www/video/controler.php?task_id=ak&s=0&d=0&m=0&p=0
2 rewrite : /xampp/www/swimming-pool/r1505ab.html -> /xampp/www/vide/controler.php?task_id=ab&r=1505
3 rewrite : /xampp/www/quick-contact/bb.html -> /xampp/www/video/controler.php?task_id=bb

My actual rule :
RewriteRule ^(.*)/(.+)([a-z]+)([^a-z][0-9]*)(.*)([a-z]*)\.html$ controler.php?$2&$3=$4$5 [NC,QSA]

It doesn't work for cases 2 and 3.
It doesn't loop entirely for case 1, as &s=0&d=0&m=0 is missing, the result is :
/xampp/www/news-summer/s0d0m0p0ak.html -> /xampp/www/video/controler.php?task_id=ak&p=0

Thanks in advance for your support.
Have a nice day.
Jean-Michel

jdMorgan

4:29 pm on Jan 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You will need three rules to do this in an efficient manner -- One rule for each case.

Jim

enghien

4:40 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



Thanks Jim for your reply.

I was afraid of that.
Can I ask you some help to correct the rule for case 3

Thanks a lot again
Jean-Michel

jdMorgan

9:00 pm on Jan 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# 1a rewrite : /news-summer/s0d0m0p0ak.html -> /video/controler.php?task_id=ak&s=0&d=0&m=0&p=0
RewriteRule ^[^/]+/([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)\.html$ /video/controler.php?task_id=$9&$1=$2&$3=$4&$5=$6&$7=$8 [NC,L]
#
# 1b rewrite : /news-summer/s0d0m0ak.html -> /video/controler.php?task_id=ak&s=0&d=0&m=0
RewriteRule ^[^/]+/([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)\.html$ /video/controler.php?task_id=$7&$1=$2&$3=$4&$5=$6 [NC,L]
#
# 1c rewrite : /news-summer/s0d0ak.html -> /video/controler.php?task_id=ak&s=0&d=0
RewriteRule ^[^/]+/([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)\.html$ /video/controler.php?task_id=$5&$1=$2&$3=$4 [NC,L]
#
# 2 rewrite : /swimming-pool/r1505ab.html -> /video/controler.php?task_id=ab&r=1505
RewriteRule ^[^/]+/([a-z]+)([0-9]+)([a-z]+)\.html$ /video/controler.php?task_id=$3&$1=$2 [NC,L]
#
# 3 rewrite : /quick-contact/bb.html -> /video/controler.php?task_id=bb
RewriteRule ^[^/]+/([a-z]+)\.html$ /video/controler.php?task_id=$1 [NC,L]

Note: I cannot resolve your statement that some of your rules worked with the apparent need to include "/video" in the substitution URL, as indicated by your example URL-paths. There may be other factors outside of the code that you posted which explain this. But as a result, you may need to adjust the paths in the substitution URL-paths shown above.

Posting here may cause the lines of code to wrap. However, each rule must be all on one line in the actual .htaccess code.

Jim

enghien

12:30 am on Jan 5, 2008 (gmt 0)

10+ Year Member


Thank you Jim,

All 3 rules are OKAY with somes ajustments around /video. Some more details perhaps will help you understand my problem.

# A- /video corresponds to a physical folder, you're right.
# B- r means $ref as article ref. or category ref.
# C- task_id is the script nr.
# D- s means start page nr.
# E- Comment the line if the script is not installed in a sub-directory of the web server root SERVER['DOCUMENT_ROOT']
RewriteBase /xampp/www/video/

# ------------ Case 3 : legal, contact... ---------------
# 3 rewrite : /quick-contact/bb.html -> /video/controler.php?task_id=bb
RewriteRule ^[^/]+/([a-z]+)\.html$ /video/controler.php?task_id=$1 [NC,L]

# ------------ Case 2 : article ---------------
# 2a rewrite : /swimming-pool/r1505ab.html -> /video/controler.php?task_id=ab&r=1505
RewriteRule ^[^/]+/([a-z]+)([0-9]+)([a-z]+)\.html$ /video/controler.php?task_id=$3&$1=$2 [NC,L]
#
# 2b rewrite : /swimming-pool/r1505s0ab.html -> /video/controler.php?task_id=ab&r=1505&s=0
RewriteRule ^[^/]+/([a-z]+)([0-9]+)([a-z]+)\.html$ /video/controler.php?task_id=$3&$1=$2 [NC,L]

# ------------ Case 1 : category ---------------
# 1- rewrite : /news-summer/r1505&s0d0m0p0ak.html -> /video/controler.php?task_id=ak&r=1505&s=0&d=0&m=0&p=0
RewriteRule ^[^/]+/([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)\.html$ /video/controler.php?task_id=$11&$1=$2&$3=$4&$5=$6&$7=$8&$9=$10 [NC,L]
#
# 1a rewrite : /news-summer/s0d0m0p0ak.html -> /video/controler.php?task_id=ak&s=0&d=0&m=0&p=0
RewriteRule ^[^/]+/([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)\.html$ /video/controler.php?task_id=$9&$1=$2&$3=$4&$5=$6&$7=$8 [NC,L]
#
# 1b rewrite : /news-summer/s0d0m0ak.html -> /video/controler.php?task_id=ak&s=0&d=0&m=0
RewriteRule ^[^/]+/([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)\.html$ /video/controler.php?task_id=$7&$1=$2&$3=$4&$5=$6 [NC,L]
#
# 1c rewrite : /news-summer/s0d0ak.html -> /video/controler.php?task_id=ak&s=0&d=0
RewriteRule ^[^/]+/([a-z]+)([0-9]+)([a-z]+)([0-9]+)([a-z]+)\.html$ /video/controler.php?task_id=$5&$1=$2&$3=$4 [NC,L]
#
# 1d rewrite : /news-summer/s0ak.html -> do not exist for me
#
# 1e rewrite : /news-summer/ak.html -> do not exist for me

I'm searching a way to make the rewriting process as quick as possible ( 1Go database, 1Mio visitors/month) :
- do you see any possibility to compress the last 4 rules 1- 1a 1b 1c into a single one?
- it seems to me the kit of rules is not optimized. What direction could you advice? Re-think the rewriting rules? Other?

Thanks a lot again.
Jean-Michel

jdMorgan

1:02 am on Jan 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It seems to me the kit of rules is not optimized. What direction could you advice? Re-think the rewriting rules?

I don't think I'd worry about it until you have more than 400 rules in the file... :)

Do you use any PHP or PERL? They are no faster than this code, and most people don't worry about using them to "power" their entire site.

Jim

enghien

10:46 am on Jan 5, 2008 (gmt 0)

10+ Year Member


Hi Jim,

Thanks for this clear answer.
For the moment, you're right, I do not have more than 400 rules in the file... :[smilestopper])

Do you use any PHP or PERL?
Yes : PHP 5.2, APACHE 2.2.6, MySQL 5.0.
For PERL, it's available but I do not use it.
Do you see some perspectives because I use PHP or PERL?

Jim

jdMorgan

2:12 pm on Jan 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Do you see some perspectives because I use PHP or PERL?

Yes, you are apparently worried about the efficiency of using four lines of RewriteRules. This is equivalent to being worried about using four lines of PHP code -- They are comparable.

In both cases --mod_rewrite and PHP-- it is good to be concerned about using efficient code overall, but that concern should be moderated; It is good to seek efficient coding, but four lines of code is not going to greatly affect performance in either case.

Jim

enghien

4:23 pm on Jan 5, 2008 (gmt 0)

10+ Year Member



Jim,

I would like to thank you so much for your clear answers and your engagement to help me quickly.

I will follow your advice and spend my time to other nice things around PHP...

Great forum.
Have a nice day.
Jean-Michel

jdMorgan

4:17 am on Jan 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A bien-tot!

Jim

enghien

7:44 pm on Jan 8, 2008 (gmt 0)

10+ Year Member



Hi Jim (bonjour!),

Once again, I have troubles with paths. I'm searching a set of 18 linear rules + 3 recursive rules to :
1- have an evolutive set of rules (no modification if query string is modified : .php?r=1505 may become .php?r=1505&s=12&m=3
2- keep the same rules for both development & production machine, just modifying RewriteBase line from RewriteBase /xampp/www/video/ to RewriteBase /video/

# 1 rewrite : /xampp/www/news-summer/r1505ab.html -> /xampp/www/video/salon/article.php?r=1505
RewriteRule ^(.*)/(.*)ab\.html$ salon/video/article-$2\.html [L]
# 2 rewrite : /xampp/www/news-winter/r1505ac.html -> /xampp/www/video/service/note.php?r=1505
RewriteRule ^(.*)/(.*)ac\.html$ salon/video/note-$2\.html [L]
# ...
# 17 rewrite : /xampp/www/news-summer/r1505aq.html -> /xampp/www/video/salon/comment.php?r=1505
# 18 rewrite : /xampp/www/news-summer/r1505s12aq.html -> /xampp/www/video/salon/comment.php?r=1505&s=12
RewriteRule ^(.*)/(.*)aq\.html$ salon/video/comment-$2\.html [L]
#
# Recursive rules
RewriteRule ^(.*)-(.+)([a-z]+)([0-9]+)([^-]*)\.html$ $1-$2&$3=$4$5\.html [N]
RewriteRule ^(.*)-([a-z]+)([0-9]+)(.*)\.html$ $1.php?$2=$3$4 [L]
RewriteRule ^(.*).html$ $1.php [L]

The result for the first rule is a repetition of the path 'video'.
/xampp/www/video/video/salon/article.php?r=1505
I cannot understand why. Please, could you help me?

Jean-Michel

jdMorgan

8:04 pm on Jan 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The path-part declared in RewriteBase should not be included in the RewriteRule.

You might consider using only "RewriteBase /xampp/www/" for development, and then commenting it out for production.

Jim

enghien

8:19 pm on Jan 8, 2008 (gmt 0)

10+ Year Member



I have also forgotten this rule that must work with the previous ones

# 19 rewrite : /xampp/www/quick-contact/bb.html -> /xampp/www/video/controler.php?task_id=bb
RewriteRule ^[^/]+/([a-z]+)\.html$ /video/controler.php?task_id=$1 [NC,L]

Jean-Michel

enghien

9:06 pm on Jan 8, 2008 (gmt 0)

10+ Year Member



Thanks a lot.
It works. Just now test deeply...

Thanks.
Jean-Michel

enghien

1:29 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Hi Jim,

I would like to add country information inside the url, just after hostname.

So transform this set of rules which works :
# rewrite : /xampp/www/news-summer/r1505ab.html -> /xampp/www/video/salon/article.php?r=1505
RewriteRule ^(.*)/(.*)ab\.html$ salon/video/article-$2\.html [L]
RewriteRule ^(.*)-([a-z]+)([0-9]+)(.*)\.html$ $1.php?$2=$3$4 [L]
RewriteRule ^(.*).html$ $1.php [L]

into this one, that fails
# rewrite : /xampp/www/uk/news-summer/r1505ab.html -> /xampp/www/video/salon/article.php?r=1505&idiom=uk
RewriteRule ^(.*)/(.*)ab\.html$ salon/video/article-$2\.html [L]
RewriteRule ^([a-z]+)/(.*)-([a-z]+)([0-9]+)(.*)\.html$ $2.php?$3=$4$5&idiom=$1 [L]

Thanks for your help
Jean-Michel

jdMorgan

2:16 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Pattern for two-letter country-code: "[a-z]{2}"

Do not use more than one ".*" pattern per rule unless your server will never have more than a few visitors per second. Multiple ".*" patterns are extremely inefficient to process. Use negative-match patterns instead. See "Regular-expressions patterns" discussion here [webmasterworld.com].

Jim

[edit]Added link to previous rewrite thread[/edit]

[edited by: jdMorgan at 2:29 pm (utc) on Jan. 21, 2008]

enghien

9:38 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Hi Jim,

1-Thanks for this interesting information (do not use more than one ".*" pattern per rule), but how can you explain this? Is it your expérience? Does this overload too much the processor?

2-How can I integrate this fact (do not use more than one ".*" pattern per rule) and my wish to have evolutive rules (i.e. that work with a longuer query string) and with my 4 actual rules :

rewrite : /xampp/www/uk/news-summer/r1505s12m3aq.html -> /xampp/www/video/salon/comment.php?r=1505&s=12&m=3&idiom=uk
RewriteRule ^(.*)/(.*)aq\.html$ salon/video/comment-$2\.html [L]
RewriteRule ^(.*)-(.+)([a-z]+)([0-9]+)([^-]*)\.html$ $1-$2&$3=$4$5\.html [N]
RewriteRule ^(.*)-([a-z]+)([0-9]+)(.*)\.html$ $1.php?$2=$3$4 [L]
RewriteRule ^(.*).html$ $1.php [L]

Jean-Michel