Forum Moderators: phranque
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
# 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]
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
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
It seems to me the kit of rules is not optimized. What direction could you advice? Re-think the rewriting rules?
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
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
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
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
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
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]
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