Search code examples
regexmod-rewriteurl-rewritingregex-greedy

RewriteCond matches greedily


I have a RewriteCond to set the current working directory in an environment variable as below:

RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*?/)\1$
RewriteRule ^(.*)$ - [E=CWD:%2]

Above this two rules , I have a separate rule for rewriting .

RewriteRule ^(?!ui?)([a-zA-Z]{2})/(.*)$  $2?language=$1 [NC,QSA]

Overall , my .htaccess section looks something like:

RewriteRule ^(?!ui?)([a-zA-Z]{2})/(.*)$  $2?language=$1 [NC,QSA]

RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*?/)\1$
RewriteRule ^(.*)$ - [E=CWD:%2]

For the REQUEST_URI :

/es/website-design/do-it-yourself-website-builder.php

CWD is set with the value '/es/'

but for the REQUEST_URI :

/de/website-design/do-it-yourself-website-builder.php

CWD is set with the value '/de/website-design/'

Ideally CWD is expected to store the base folder name ('/es/' and '/de/' respectively) in the abovementioned cases.

What is going wrong here ? I am stuck on this for quite sometime now.

Please help.

The contents of my .htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond $2 !^(index\.php)
RewriteRule ^(shop)/(.*)$  $2 [NC,QSA]

RewriteCond %{THE_REQUEST} ^.*/(shop)/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,QSA,L]

RewriteRule ^(?!ui?)([a-zA-Z]{2})/(.*)$  $2?language=$1 [NC,QSA]

# following tweak done for running supersite from within a folder
# for ever request url , this rule will match out the base folder
# name and capture it in the regex rule given in RewriteCond
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*?/)\1$
RewriteRule ^(.*)$ - [E=CWD:%2]

# Preventing Hot Linking of images
#RewriteCond %{REQUEST_URI} ^\/getImage\.php [NC]
#RewriteCond %{HTTP_REFERER} !^.*(manage.bigrock.in|manage.bigrock.com).*$ [NC]
#RewriteCond %{REQUEST_URI} .*\.css$ [NC,OR]
#RewriteCond %{REQUEST_URI} .*\.js$ [NC,OR]
#RewriteCond %{QUERY_STRING} !^src\=favicon\.ico$ [NC]
#RewriteCond %{HTTP_REFERER} ^http://([^/]+) [OR]
#RewriteCond %{HTTP_REFERER} ^$
#RewriteCond %{HTTP_HOST}<>%1 !^(.+)<>\1$ [NC]
#RewriteRule ^(.*)$ /noimage.html [Last]

RewriteCond %{HTTP_USER_AGENT} ^GlobalSign-Approver
RewriteRule ^(.*)$ cloudflare.html [L]

RewriteCond %{SERVER_PORT} 80
RewriteRule ^/onlinepayment/(.*)$  https://%{HTTP_HOST}/onlinepayment/$1  [NC,QSA]

RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^index\.php$ / [R=301,QSA,L]

RewriteCond %{REQUEST_FILENAME} (S|s)itemap.xml
RewriteRule ^(.*)$  /seo-files.php?type=sitemap  [L]

RewriteCond %{REQUEST_FILENAME} ror.xml
RewriteRule ^(.*)$  /seo-files.php?type=ror [L]

RewriteCond %{REQUEST_FILENAME} urllist.txt
RewriteRule ^(.*)$  /seo-files.php?type=urllist [L]

# Robots.txt redirection to robots.php
RewriteRule ^robots.txt robots.php [L,NC]

# URL rewriting
RewriteRule ^domain-registration-pricing$ /domain-registration/domain-registration-price.php [R=302,L,NC,QSA]
RewriteRule ^domain-registration/transfer/index.php /domain.php?action=domain_transfer&bypass_url_mapper=false [L,NC,QSA]

RewriteCond %{QUERY_STRING} ^idn
RewriteRule .*domain-registration/index.php /domain-registration/idn? [R=301,L]

RewriteRule ^promos.php /content.php?action=promos [L,NC,QSA]

RewriteRule ^website-design/index.php /sitebuilder.php?type=sblite&bypass_url_mapper=false [L,NC,QSA]
#RewriteRule ^website-design/do-it-yourself-website-builder.php  %{ENV:CWD}website-design/index.php? [R=301,L,NC]
RewriteRule ^website-design/web-design-service.php /website-design/index.php [R=301,L,NC,QSA]
RewriteRule ^website-design/(.*).js$ /$1.js [L,NC,QSA]

RewriteRule ^website-builder.php$ /website-design/index.php [R=301,L,NC,QSA]
RewriteRule ^do-it-yourself.php$ /website-design/index.php [R=301,L,NC,QSA]
RewriteRule ^build-it-for-me.php$ /website-design/index.php [R=301,L,NC,QSA]

RewriteRule ^combo-offers /product-bundle.php [L,NC,QSA]

RewriteRule ^express-cart[/]{0,1}([a-zA-Z0-9_]*)[/]{0,1}$ /quickbuy.php?action=$1 [L,NC,QSA]
RewriteRule ^express-cart[/]{0,1}([a-zA-Z0-9_]*)[/]{1}([a-zA-Z0-9_]*)[/]{0,1}$ /quickbuy.php?action=$1_$2 [L,NC,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /urlmapper.php [L,QSA]

Solution

  • Make sure every rule has L flag e.g.

    RewriteRule ^(?!ui?)([a-zA-Z]{2})/(.*)$  $2?language=$1 [NC,QSA]
    

    should be

    RewriteRule ^(?!ui?)([a-zA-Z]{2})/(.*)$  $2?language=$1 [NC,L,QSA]
    

    and then completely clear your browser cache.

    Env variable CWD should always be coming as / because htaccess is placed directly in site root directory.