Forum Moderators: phranque
Hope someone can guide on this :
I have an url like this :
[#*$!.xxx.com.hk...]
or
[xxx.xxx.com.hk...]
I like the final redirected url to be :
[xxx.xxx.com.hk...]
or
[xxx.xxx.com.hk...]
Notice that /sg/ has been substituted with /hk/. Notice the number of forward slashes after /sg/ can be different.
RewriteCond %{HTTP_HOST} ^xxx\.xxx\.com\.(.+) [NC]
RewriteRule /cms/export/(.+)/(/¦,¦[a-z]¦[A-Z]¦?¦\.*) /cms/export/%1/$2 [R=301,L]
I managed to substitute the /sg/ with /hk/ but I just can't seem to substiture the entire string (including unknown number of slashes) after /sg/ to end of line.
Its always only the string after the last slash (ie. index.html) gets substituted.
Can someone show me the right way?
thanks very much
Welcome to WebmasterWorld!
It looks like your pattern was too specific -- and too complicated.
I modified this using negative-lookahead matches. The pattern will match anything up to the specified character. For example, in the RewriteCond, ([^:]+) will match your TLD up to a colon preceding an optional port number, i.e. example.com.uk:80
In the RewriteRule, the first variable pattern [^/]+ matches anything up to the next slash that it finds. Because this value (sg) is not used in a back-reference and contains no alterates (A¦B), there is no need to enclose it in parentheses.
The next variable pattern ([^.+]) matches everything between "sg/" and the first period that it finds, which should be the start of ".html". This is then back-referenced as $1.
RewriteCond %{HTTP_HOST} ^www\.example\.com\.([^:]+) [NC]
RewriteRule /cms/export/[^/]+/([^.]+)\.html$ /cms/export/%1/$1.html [R=301,L]
Jim
RewriteEngine on
RewriteRule ^/(abc/export)/sg/(.*) /$1/hk/$2 [L,R=301]
RewriteEngine on
RewriteMap tolower int:tolower
RewriteCond %{HTTP_HOST}!^$
RewriteCond %{HTTP_HOST} \.([a-z]+)$ [NC]
RewriteRule ^/(abc/export)/sg/(.*) /$1/${tolower:%1}/$2 [L,R=301]
RewriteEngine on
RewriteMap tolower int:tolower
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^(.*) /defaultpage.html?$1 [L]RewriteCond %{HTTP_HOST} \.([a-z]+)$ [NC]
RewriteRule ^/(abc/export)/sg/(.*) /$1/${tolower:%1}/$2 [L,R=301]
You could then have 'defaultpage.html' (or defaultpage.php, or what have you) return a friendly error, possibly using the originally requested path in the error text.
Does all that make sense, more or less?
Two points of related discussion:
1) A check for a blank host is usually not needed with a positive hostname pattern-match. It's required, though, for a negative hostname pattern-match.
That is, if you rewrite/redirect only when (hostname = somevalue), then a blank hostname won't ever accidentally match a non-blank pattern. However, if you redirect when (hostname NOT = somevalue), then a blank hostname from an HTTP/1.0 client will likely put you into an "infinite" redirection loop unless you add the RewriteCond to explicitly check for a non-blank hostname.
2) A very minor one: I like to use "." instead of "!^$" for non-blank-matching. It's two characters shorter, plus then you don't have to do a work-around to avoid this forum removing the space between "}" and "!" when posting. :)
Jim
I like to use "." instead of "!^$" for non-blank-matching. It's two characters shorter, plus then you don't have to do a work-around to avoid this forum removing the space between "}" and "!" when posting. :)
Yeah, I'd noticed that. I prefer the "!^$" syntax because (to my eyes, anyway) it's a bit more obvious what it's doing. The '.' I'd have to think about for a second the first couple of times I saw it. But that may just be me. As for the latter point, true. Although I'd argue that it was a bug in the forumcode, since the <pre> </pre> tags are supposed to Do The Right Thing(tm) with whitespace. *points* at the forum source code. Go! Fix! =D
I followed your regular expression but keep getting Redirection limit reached for it. I don't understand why it keeps going into a loop (despite putting in L flag).
Below is the portion of the httpd.conf
RewriteCond %{REQUEST_METHOD} ^(TRACE¦TRACK)
RewriteRule .* - [F,L]
RewriteCond %{HTTP_HOST} ^www\.#*$!\.com\.([^:]+) [NC]
RewriteRule /cms/export/[^/]+/([^.]+)\.html$ /cms/export/%1/$1.html [R=301,L
]
RewriteRule /web-console.*$ - [F,L]
RewriteRule /jmx-console.*$ - [F,L]
thanks a million
RewriteLog /path/to/rewrite.log
RewriteLogLevel 9
Three possibly-easy fixes:
First, you could change the host
RewriteCond %{HTTP_HOST} ^www\.example\.com\.([^:]+) [NC]
RewriteRule /cms/export/[^/]+/([^.]+)\.html$ http://www.example.[b]com/cms[/b]/export/%1/$1.html [R=301,L]
Or, you can change the URL-path:
RewriteCond %{HTTP_HOST} ^www\.example\.com\.([^:]+) [NC]
RewriteRule /cms/export/[^/]+/([^.]+)\.html$ http://%{HTTP_HOST}/cms/expor[b]ts/[/b]%1/$1.html [R=301,L]
Or, if the original URL *always* has "sq" in it, then explicitly test for that:
RewriteCond %{HTTP_HOST} ^www\.example\.com\.([^:]+) [NC]
RewriteRule /cms/export/[b]sq[/b]/([^.]+)\.html$ http://%{HTTP_HOST}/cms/export/%1/$1.html [R=301,L]
Jim