Redirect from an old domain with a subdirectory to a new domain without the subdirectory

Published by

on

In this example, we want to forward the entire sub directory from one domain name to another domain name without the sub directory.

For example:

From https://abc.byu.edu/subfolder/

To https://def.byu.edu

We will use .htaccess to create the redirect.

# enable mod_rewrote
RewriteEngine On
RewriteBase /

# I try to use following line but it doesn’t work
RewriteCond %{REQUEST_URL} ^/yoursubfoldername/(.*)$

# I change it to following line, but it also doesn’t work
# RewriteCond %{HTTP_HOST} ^newdomainname\.byu.\edu [NC]

Therefore,
# enable mod_rewrote
RewriteEngine On
RewriteBase /
RewriteRule ^forwardedfoldername/(.*)$ https://newdomainname.byu.edu/$1 [R=301,L]

# R = 301 = R is redirect and 301 is permanent redirect
# [NC] = it is case insensitive
# [L] = stop processing further ruler
# ^ = start pf string