I have a website for which I would like to redirect the following requests :
www.example.com/wwv_flow.js_messages?p_app_id=103&p_lang=fr&p_version=19.1.0.00.15-51560988
To :
www.example.com/apex/app/wwv_flow.js_messages?p_app_id=103&p_lang=fr&p_version=19.1.0.00.15-51560988
Basically, the rule is whenever the requests starts with wwv, then add apex/app/ right before and keep everything the same.
In my .htaccess
file I tried :
RewriteCond %{HOST_NAME} ^wwv([.]+)/?$
RewriteRule ^wwv([.]+)/?$ ./apex/app/$1 [L]
But it's not working. First the condition doesn't work, and second the redirection fails due to the special characters in the URL : ?, &, =
Does anyone know how to solve that please?
Best Answer
This rule block will never match the stated URL for several reasons:
[.]
(a dot inside a character class) matches a literal dot, not "any character". The character class negates the dots special meaning. So,[.]+
matches 1 or more dots. You should remove the square brackets, ie..+
in order to match 1 or more "any character". Although to match any request that simply "starts withwwv
" (as stated in your question), the entire regex can be simplified to^wwv(.*)
.RewriteCond
directive it looks like you are trying to match thewwv
part against the hostname. In your example URLwwv
is part of the URL-path, not the hostname.HOST_NAME
. Perhaps you meantHTTP_HOST
? However, that is still incorrect (as mentioned above). (You should have usedREQUEST_URI
- in order to match against the URl-path - but then the regex would still have been incorrect. However, this condition is superfluous anyway.)The
RewriteCond
(condition) directive is not required here. (Repeating the same comparison in theRewriteCond
that you are performing in theRewriteRule
directive is superfluous and just results in more "work".)The
./
prefix on theRewriteRule
substitution is not required here and should be removed.Note that the
RewriteRule
pattern matches against the URL-path only. This notably excludes the query string. (If you specifically needed to match against the query string then you would need aRewriteCond
directive that matched against theQUERY_STRING
server variable. But this is not required here.)These aren't really "special characters" and they should not cause a problem here. It fails because it simply doesn't do anything (as noted above) - it doesn't match the requested URL.
The query string is passed through to the substitution by default, so there is nothing you need to do regarding the "special characters".
Solution
To perform the necessary "redirect" you would need something like the following:
The
(.*)
capturing subpattern matches the remainder of the URL-path, ie._flow.js_messages
in your example. And this is then referenced in the substitution with the$1
backreference.As noted above, the query string (ie.
p_app_id=103&p_lang=fr&p_version=19.1.0.00.15-51560988
) is passed through to the substitution by default. There is nothing extra you need to do here.Note that this is a 302 (temporary) redirect.
If you require an internal rewrite (as suggested by your code snippet) then remove the
R
flag. And optionally remove the slash prefix on theRewriteRule
substitution (but this can depend on the value ofRewriteBase
- if any).Note that the order of these directives in
.htaccess
is important. If you have existing mod_rewrite directives, then this will likely need to go near the top.You don't actually need mod_rewrite, if this is the only action you are performing. A mod_alias
RedirectMatch
would suffice. However, mod_rewrite would probably be necessary if you have existing mod_rewrite directives.