I need to redirect whole website from one domain to another with the exact extention, BUT only all pages in specific directory should be redirected to another directory on new website. http://www.website.com/dir/page-123 (all pages starting with "page-*" to http://www.newwebsite/dir/. Tried many options, nothing works.


RewriteRule ^dir/(.+?)(-[0-9]+)?$ oldwebsite.com/dir
RewriteRule (.*) newwebsite.com/$1 [R=301,L]

1 Answer 1

RewriteRule ^dir/(.+?)(-[0-9]+)?$ oldwebsite.com/dir
RewriteRule (.*) newwebsite.com/$1 [R=301,L]

Not sure what you thought these directives were doing, or why you were using two directives and rewritting to the oldwebsite in the first one?

If you need to redirect to a different host, then you must specify an absolute URL, including the scheme, in the substitution. eg. http://newwebsite.com/.

But the last rule takes control on first one

Yes, it would, since you don't include the L (LAST) flag on the first RewriteRule and the second rule matches everything. RewriteRule directives chain together (the output of the first directive is used as the input for the second, etc.), so you need to stop that from happening.

However, you presumably need these to be external Redirects, not internal rewrites.

Try the following in the .htaccess file in the specific subdirectory of oldwebsite.com that you want to redirect from (eg. example.com/dir/.htaccess).

RewriteEngine On
RewriteRule ^(page-.*) http://newwebsite.com/newdir/ [R=301,L]

This redirects all pages that start with "page-" in the subdirectory (eg. dir) to http://newwebsite.com/newdir/.

  • 1
    Thank you @w3dk - new dir with htaccess worked as magic!
    – alya
    Sep 12, 2016 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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