RewriteCond problem with trailing slash
4 posts
• Page 1 of 1
- jfrobishow
- Posts: 3
- Joined: 22 Feb 2016, 20:51
RewriteCond problem with trailing slash
Hi,
I'm having a strange issue with one of my rule which (to me) appears to incorrectly include a trailing slash in the wrong captured group.
So far I have
What I'm trying to accomplish is to redirect some URLs when the 3rd group is not found in my map file.
e.g.
http://local.site/newsite/news/mynewstitle/ should go to http://local.site/news/mynewstitle/ since mynewstitle isn't in my mapfile
http://local.site/newsite/news/en/ should not be redirected since en is in my mapfile.
If I go to
http://local.site/newsite/news/en without a trailing slash everything works and I'm not redirected.
If however, I include a trailing slash
http://local.site/newsite/news/en/ I would expect my groups to be
$1 = newsite
$2 = news
$3 = en
$4 = /
Since I'm checking for a map match on $3 it should match, it's passing in en/ as $3 and I get a match fail so the redirect is executed.
I've been trying various combination to no avail, I can't get rid of my trailing slash in $3.
Ideas? Thanks in advance!
I'm having a strange issue with one of my rule which (to me) appears to incorrectly include a trailing slash in the wrong captured group.
So far I have
- Code: Select all
RewriteBase
RewriteMap weblangidmap txt:C:/inetpub/www/site/weblangcode.txt
RewriteCond %{HTTP:Host} ^(?:www\.)?local\.site$
RewriteCond %{REQUEST_URI} ^/(.*)/(news|newsletter)/([a-z]*)(/?)
RewriteCond ${weblangidmap:$3|NOT_FOUND} NOT_FOUND
RewriteRule ^/(.*)/(news|newsletter)/(.*) /$2/$3 [R=301,NC,L]
What I'm trying to accomplish is to redirect some URLs when the 3rd group is not found in my map file.
e.g.
http://local.site/newsite/news/mynewstitle/ should go to http://local.site/news/mynewstitle/ since mynewstitle isn't in my mapfile
http://local.site/newsite/news/en/ should not be redirected since en is in my mapfile.
If I go to
http://local.site/newsite/news/en without a trailing slash everything works and I'm not redirected.
- Code: Select all
127.0.0.1 127.0.0.1 Mon, 22-Feb-2016 18:57:57 GMT [local.site/sid#2][rid#19918888/initial] (4) RewriteCond: input='local.site' pattern='^(?:www\.)?local\.site$' => matched
127.0.0.1 127.0.0.1 Mon, 22-Feb-2016 18:57:57 GMT [local.site/sid#2][rid#19918888/initial] (4) RewriteCond: input='/newsite/news/en' pattern='^/(.*)/(news|newsletter)/([a-z]*)(/?)' => matched
127.0.0.1 127.0.0.1 Mon, 22-Feb-2016 18:57:57 GMT [local.site/sid#2][rid#19918888/initial] (5) map lookup OK: map=weblangidmap[txt] key=en -> val=1
127.0.0.1 127.0.0.1 Mon, 22-Feb-2016 18:57:57 GMT [local.site/sid#2][rid#19918888/initial] (4) RewriteCond: input='1' pattern='NOT_FOUND' => not-matched
If however, I include a trailing slash
http://local.site/newsite/news/en/ I would expect my groups to be
$1 = newsite
$2 = news
$3 = en
$4 = /
Since I'm checking for a map match on $3 it should match, it's passing in en/ as $3 and I get a match fail so the redirect is executed.
- Code: Select all
127.0.0.1 127.0.0.1 Mon, 22-Feb-2016 19:13:31 GMT [local.site/sid#2][rid#19917832/initial] (4) RewriteCond: input='local.site' pattern='^(?:www\.)?local\.site$' => matched
127.0.0.1 127.0.0.1 Mon, 22-Feb-2016 19:13:31 GMT [local.site/sid#2][rid#19917832/initial] (4) RewriteCond: input='/newsite/news/en/' pattern='^/(.*)/(news|newsletter)/([a-z]*)(/?)' => matched
127.0.0.1 127.0.0.1 Mon, 22-Feb-2016 19:13:31 GMT [local.site/sid#2][rid#19917832/initial] (5) map lookup FAILED: map=weblangidmap[txt] key=en/
127.0.0.1 127.0.0.1 Mon, 22-Feb-2016 19:13:31 GMT [local.site/sid#2][rid#19917832/initial] (4) RewriteCond: input='NOT_FOUND' pattern='NOT_FOUND' => matched
127.0.0.1 127.0.0.1 Mon, 22-Feb-2016 19:13:31 GMT [local.site/sid#2][rid#19917832/initial] (1) escaping /news/en/
127.0.0.1 127.0.0.1 Mon, 22-Feb-2016 19:13:31 GMT [local.site/sid#2][rid#19917832/initial] (2) explicitly forcing redirect with http://local.site/news/en/
127.0.0.1 127.0.0.1 Mon, 22-Feb-2016 19:13:31 GMT [local.site/sid#2][rid#19917832/initial] (2) internal redirect with /newsite/news/en/ [INTERNAL REDIRECT]
I've been trying various combination to no avail, I can't get rid of my trailing slash in $3.
Ideas? Thanks in advance!
- jfrobishow
- Posts: 3
- Joined: 22 Feb 2016, 20:51
Re: RewriteCond problem with trailing slash
SOLVED
I was expecting the $3 I use in my second RewriteCond to come from the first RewriteCond but that's not the case. It's populated from the RewriteRule so its including the trailing slash since that group was more permissive.
RewriteCond %{REQUEST_URI} ^/(.*)/(news|newsletter)/([a-z]*)(/?)
RewriteCond ${weblangidmap:$3|NOT_FOUND} NOT_FOUND
RewriteRule ^/(.*)/(news|newsletter)/(.*) /$2/$3 [R=301,NC,L]
I've changed it to this and now everything works as expected:
I was expecting the $3 I use in my second RewriteCond to come from the first RewriteCond but that's not the case. It's populated from the RewriteRule so its including the trailing slash since that group was more permissive.
RewriteCond %{REQUEST_URI} ^/(.*)/(news|newsletter)/([a-z]*)(/?)
RewriteCond ${weblangidmap:$3|NOT_FOUND} NOT_FOUND
RewriteRule ^/(.*)/(news|newsletter)/(.*) /$2/$3 [R=301,NC,L]
I've changed it to this and now everything works as expected:
- Code: Select all
RewriteCond %{REQUEST_URI} ^/(.*)/(news|newsletter)/([^/]+)(.*)
RewriteCond ${weblangidmap:$3|NOT_FOUND} NOT_FOUND
RewriteRule ^/(.*)/(news|newsletter)/([^/]+)(.*) /$2/$3$4 [R=302,NC,L]
Re: RewriteCond problem with trailing slash
Hello.
Just FYI: back-references to RewriteCond rules use %1, %2... etc. syntax instead of $1... Submatches are numbered through all set of RewriteCond-s, i.e. if the first RewriteCond has one submatch and second cond has two submatches they can be referenced in substitution string as %1, %2 and %3 respectively.
Just FYI: back-references to RewriteCond rules use %1, %2... etc. syntax instead of $1... Submatches are numbered through all set of RewriteCond-s, i.e. if the first RewriteCond has one submatch and second cond has two submatches they can be referenced in substitution string as %1, %2 and %3 respectively.
- jfrobishow
- Posts: 3
- Joined: 22 Feb 2016, 20:51
Re: RewriteCond problem with trailing slash
@Yaroslav
I didn't know that! Thanks, much appreciated.
I didn't know that! Thanks, much appreciated.
4 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 29 guests