Redirect a directory to a new domain without touching the main website

.htaccess301-redirectapache-2.2mod-rewriteredirect

I would like to redirect https://mineyourmind.de/forum to https://mineyourmind.net/forum .

Every rewrite rule I could google didn´t work, they also redirected the main domain (mineyourmind.de).

Im redirecting http:// www. and http:// and https:// via the apache siteconfig.

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.mineoyurmind\.de$ [NC]
RewriteRule (.*) https://mienoyurmind.de%{REQUEST_URI} [R=301,L]

The .htaccess of the forum directory looks like this:

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 500 default

<IfModule mod_rewrite.c>
    RewriteEngine On

    #   If you are having problems with the rewrite rules, remove the "#" from the
    #   line that begins "RewriteBase" below. You will also have to change the path
    #   of the rewrite to reflect the path to your XenForo installation.
    #RewriteBase /xenforo

    #   This line may be needed to enable WebDAV editing with PHP as a CGI.
    #RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
    RewriteRule ^.*$ index.php [NC,L] 
</IfModule>

Should I add the rewrite for mineyourmind.de/forum to mineyourmind.net/forum to the apache site config or to the htaccess and how should this look like ?


edit:

I tried it like this:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.mineyourmind\.de$ [NC]
RewriteRule (.*) https://mineyourmind.de%{REQUEST_URI} [R=301,L]
RewriteRule (/forum.*) https://mineyourmind.net/$1 [R=301,L]

and like this

RewriteEngine On
RewriteRule (/forum.*) https://mineyourmind.net/$1 [R=301,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.mineyourmind\.de$ [NC]
RewriteRule (.*) https://mineyourmind.de%{REQUEST_URI} [R=301,L]

both isnt working

Best Answer

Here's a rule to redirect every request that starts with "/forum".

RewriteRule (/forum.*) https://mineyourmind.net/$1 [R=301,L]

This means "when the URI starts with "/forum", you should capture the entire URI and just replace the hostname with mineyourmind.de instead. The $1 means "what you just matched".

Edit:

To make sure that you don't also redirect any file starting with "forum" in its name, you may need two rules.

# To match /forum only:
RewriteRule ^/forum$ https://mineyourmind.net/$1 [R=301,L] 
# To match any URI under the /forum directory, e.g. "/forum/" and "/forum/someotherfile.html"
RewriteRule ^/forum/(.*) https://mineyourmind.net/forum/$1 [R=301,L]

End of edit

Whether you should do this in .htaccess or in the apache site config depends a bit on how your system is set up, i.e. where you store other site-specific documentation. Generally speaking there is a slight overhead when using .htaccess, since apache will re-read the .htaccess file for every new request, whereas site config will only be read when the server is restarted. But as a rule, I would keep all rewrite config together in the same file as far as possible, to make it easier to manage. Whether that is .htacces, or a virtualhost.conf, or httpd.conf, is up to you.

You should also read the RewriteRule documentation, which explains under what circumstances you need to include the initial / in your RewriteRule.

Related Topic