htaccess - redirect whole website to another with exact extentions but rediret directory/pages to different directory under different domain

htaccess - redirect whole website to another with exact extentions but rediret directory/pages to different directory under different domain - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, redirects, apache, , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


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.



Tried:



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

Solution :


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


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect