You are close, but there are some errors:
RewriteRule ^/.*$ ....
In .htaccess
, the URL-path matched by the RewriteRule
pattern does not start with a slash, so this should be simply ^.*$
(or even just .*
- since regex is greedy by default), not ^/.*$
. By including the slash prefix, it will never match, so the directive does nothing. (You would need the slash prefix in a server or virtualhost context.)
RewriteCond %{REQUEST_URI} ^/page/product/New-Design/* [NC,OR]
The CondPattern (2nd argument to the RewriteCond
directive) is a regex. The trailing *
looks like you are treating like a wildcard expression. The *
at the end matches the previous character 0 or more times, so in this case it is matching a slash 0 or more times - so again, this is not going to match the URLs you want to redirect. You should either use .*
(to match 0 or more of any character: the dot), or omit it entirely, since you only need to match URLs that start with that URL-path.
RewriteCond %{REQUEST_URI} ^/page/product/New-Design/* [NC,OR]
RewriteCond %{REQUEST_URI} ^/page/product/nature-of-design-purchase/* [NC,OR]
RewriteRule ^/.*$ https://www.testexamplesite.com/subjects/book/business-studies/ [R=302,L]
You should never include the OR
flag on the last condition. This is essentially saying OR true
and the rule will execute unconditionally (that is, if the RewriteRule
pattern matched).
RewriteCond %{REQUEST_URI} ^/engine.jsp* [NC]
RewriteRule ^/.*$ https://www.testexamplesite.com/subjects/book/business-studies/ [R=302,L]
The REQUEST_URI
server variable does not contain the query string, so if you want to match any query string then you don't need to do anything in this respect, you just need to match the required URL-path. (To specifically match the query string you need a condition that checks against the QUERY_STRING
server variable.)
Also, where possible it is more efficient to perform the necessary URL-path checks in the RewriteRule
pattern, rather than using an additional RewriteCond
(condition) directive. So, the above could be rewritten as a single directive:
RewriteRule ^engine\.jsp$ https://www.testexamplesite.com/subjects/book/business-studies/ [R=302,L]
Remember to escape literal dots in the regex. You did state in your question that this should redirect to https://www.testexamplesite.com
, not https://www.testexamplesite.com/subjects/book/business-studies/
, as you've stated here?
In Summary
So, bringing this together, try the following instead. Note that these directives need to go before the WordPress front-controller, at the top of the .htaccess
file.
RewriteRule ^page/product/(new-design|nature-of-design-purchase)/ https://www.testexamplesite.com/subjects/book/business-studies/ [NC,R=302,L]
RewriteRule ^engine\.jsp$ https://www.testexamplesite.com/ [NC,R=302,L]
RewriteRule ^page/product/(old-design|nature-of-olddesign-purchase)/ https://www.testexamplesite.com/subjects/book/business-oldstudies/ [R=302,L]
Note that I removed the end-of-string anchor ($
) from the RewriteRule
pattern. And changed the pattern to lowercase, since you have the NC
(nocase
) flag anyway.
UPDATE#1: To redirect only when a query string is present on the /engine.jsp
URL you’ll need to add a condition to the middle rule that checks against the QUERY_STRING
server variable. For example:
RewriteCond %{QUERY_STRING} .
RewriteRule ^engine\.jsp$ https://www.testexamplesite.com/ [NC,QSD,R=302,L]
The regex (a single dot) in the CondPattern ensures that the query string is present on the request.
The QSD flag (Apache 2.4+) is required to discard the query string from the redirected response.
UPDATE#2: Sorry , If I have missed to add earlier, for this requirement we are using Virtualhosts directive and not .htaccess
In that case you will need to match a slash prefix on the URL-path in the RewriteRule
pattern. This is because in a virtualhost (or server) context, the RewriteRule
directive matches against the full URL-path, not a relative URL-path (that is relative to the directory of where the .htaccess
file is contained).
So, finally we have:
RewriteRule ^/page/product/(new-design|nature-of-design-purchase)/ https://www.testexamplesite.com/subjects/book/business-studies/ [NC,R=302,L]
RewriteCond %{QUERY_STRING} .
RewriteRule ^/engine\.jsp$ https://www.testexamplesite.com/ [NC,QSD,R=302,L]
RewriteRule ^/page/product/(old-design|nature-of-olddesign-purchase)/ https://www.testexamplesite.com/subjects/book/business-oldstudies/ [R=302,L]
NB: These directives can be made to work in both contexts (ie. server and directory) by making the slash prefix in the RewriteRule
pattern optional. eg. ^/?engine\.jsp$
.
oldexamplesite.com
andtestexamplesite.com
point to different filesystems?/data/web/oldsite
for the first URL and/data/web/testsite
for the second URL