Ideally, the blog
subdomain would point to a different filesystem location (its own document root), then you could use a simple Redirect
directive in its own .htaccess
file in the root of the subdomain. For example:
Redirect 301 / https://www.example.com/ratgeber/
The Redirect
directive is prefix-matching and everything after the match is copied onto the end of the target, so https://blog.example.com/anything
is redirected to https://www.example.com/ratgeber/anything
.
However, if both hostnames point to the same place (as would seem to be the case) then you will need to use mod_rewrite at the very top of the root .htaccess
file. For example:
RewriteCond %{HTTP_HOST} ^blog\.(example\.com) [NC]
RewriteRule ^ https://www.%1/ratgeber%{REQUEST_URI} [R=301,L]
This specifically checks for the blog.example.com
hostname in the preceding condition. The %1
backreference simply saves repetition having captured the domain name in the preceding condition.
The REQUEST_URI
server variable already contains the slash prefix.
Always test first with a 302 (temporary) redirect to avoid potential caching issues. You should clear your browser cache before testing.
If you aren’t serving multiple domains (that could also have a blog
subdomain) then you can make the rule entirely generic (to avoid hardcoding the domain name), to redirect from the blog
subdomain to www
(plus subdirectory). For example:
RewriteCond %{HTTP_HOST} ^blog\.(.+?)\.?$ [NC]
: