简体   繁体   中英

Regular expression Rewrite rule

I am struggling with a mod rewrite regular expression that extracts the language from the URL and transforms it into an http parameter.

I want to rewrite mydomain/FR/workingdays_per_month_2015.htm into mydomain/tables.php?annnecal=2015&language=FR

and

mydomain/ES/workingdays_per_month_2016.htm into mydomain/tables.php?annnecal=2016&language=ES

The following rule works:

RewriteRule ^([A-Z]{2,2})/workingdays_per_month_([0-9]*).htm tables.php?anneecal=$2&language=$1  [L]

If the language is omitted in the url, I want to rewrite to default language (english): mydomain/workingdays_per_month_2015.htm into mydomain/tables.php?annnecal=2015&language=EN

so I added this rule:

 RewriteRule ^workingdays_per_month_([0-9]*).htm tables.php?anneecal=$1&language=EN  [L]

Those two rules works separately but not together.

If I visit mydomain/workingdays_per_month_2015.htm I get a 404 (not found) for this page (when the two rules are added one after another in the .htacess).

You can keep your rules like this:

RewriteEngine On

RewriteRule ^([A-Z]{2})/workingdays_per_month_([0-9]+)\.htm$ tables.php?anneecal=$2&language=$1  [L,QSA,NC]

RewriteRule ^workingdays_per_month_([0-9]+)\.htm$ tables.php?anneecal=$1&language=EN  [L,QSA,NC]

Make sure these rules are placed before your other rules.

My problem was actually coming from my Notepad++ editor running on windows and my web server running on Unix.

My end of lines were [LF].

My Unix server, expecting EOL as [CR][LF], was thus seeing just one rule, not two.

In Notepad++, using View | Show symbol | Show end of line and Edit | EOL Conversion | Unix

solved my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM