I have the following scenarios for web page redirects

1) All pages under www.oldexamplesite.com/page/product/New-Design/* and All pages under www.oldexamplesite.com/page/product/nature-of-design-purchase/*

should be redirected to https://www.testexamplesite.com/subjects/book/business-studies/

2) Pages like http://www.oldexamplesite.com/engine.jsp and http://www.oldexamplesite.com/engine.jsp?page=catalogues&portal=books should be redirected to https://www.testexamplesite.com . There are multiple pages with http://www.oldexamplesite.com/engine.jsp?* which should be redirected to https://www.testexamplesite.com

3) All pages under www.oldexamplesite.com/page/product/Old-Design/* and All pages under www.oldexamplesite.com/page/product/nature-of-olddesign-purchase/*

should be redirected to https://www.testexamplesite.com/subjects/book/business-oldstudies/ .

To implement the above scenarios I tried the following redirect rules

   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]

   RewriteCond %{REQUEST_URI} ^/engine.jsp* [NC]
   RewriteRule ^/.*$ https://www.testexamplesite.com/subjects/book/business-studies/ [R=302,L]

   RewriteCond %{REQUEST_URI} ^/page/product/Old-Design/* [NC,OR]
   RewriteCond %{REQUEST_URI} ^/page/product/nature-of-olddesign-purchase/* [NC,OR]
   RewriteRule ^/.*$ https://www.testexamplesite.com/subjects/book/business-oldstudies/ [R=302,L]

With the above rules the redirections are not happening. So, please suggest how to make the redirections work .


Point 2) Pages like http://www.oldexamplesite.com/engine.jsp?page=fruits&portal=apples and http://www.oldexamplesite.com/engine.jsp?page=catalogues&portal=books should be redirected to https://www.testexamplesite.com . Besides above two there are multiple jsp pages starting with http://www.oldexamplesite.com/engine.jsp?........ which should be redirected to https://www.testexamplesite.com but there should but no redirection for http://www.oldexamplesite.com/engine.jsp

  • Do oldexamplesite.com and testexamplesite.com point to different filesystems? – MrWhite Jul 5 '19 at 9:22
  • 1
    The documentroot for both the URLs are on the same file system but different directories – Zama Ques Jul 5 '19 at 9:25
  • Ok, but not a subdirectory of the other? – MrWhite Jul 5 '19 at 9:25
  • 1
    No.. It is like /data/web/oldsite for the first URL and /data/web/testsite for the second URL – Zama Ques Jul 5 '19 at 9:29

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$.

| improve this answer | |
  • I have updated point 2) as there is some change in our requirement but rest are working perfect – Zama Ques Jul 8 '19 at 6:27
  • Will this work for point 2) requirement RewriteCond %{REQUEST_URI} !^ ^/engine\.jsp$ RewriteRule ^/engine\.jsp\(.*) https://www.testexamplesite.com/ [NC,R=302,L] – Zama Ques Jul 8 '19 at 7:55
  • 1
    Ah, sorry, you need the QSD flag to discard the query string (assuming you are on Apache 2.4) from the redirection response. I assume you are using .htaccess? In which case you should not prefix the RewriteRule pattern with a slash. – MrWhite Jul 10 '19 at 9:54
  • 1
    Perfect!. Thanks so much. Sorry , If I have missed to add earlier, for this requirement we are using Virtualhosts directive and not .htaccess. – Zama Ques Jul 10 '19 at 14:49
  • 1
    If these directives are directly in a VirtualHost context (and not in a <Directory> container) then you will need to match a slash prefix on the URL-path. – MrWhite Jul 10 '19 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.