I am using the domains ergonomi.example and fysiarbejdsliv.example

I want fysiarbejdsliv.example to redirect to https://fysiarbejdsliv.example/dansk-selskab-for-fysioterapi-i-arbejdsliv.

I have tried a lot of solutions in htaccess, but none of them are working for me.


RewriteRule ^fysiarbejdsliv.example/(.*)$ https://www.fysiarbejdsliv.example/dansk-selskab-for-fysioterapi-i-arbejdsliv/$1 [L,R=301,QSA]

Both ergonomi.example and fysiarbejdsliv.example points to the homepage as it is now. I want fysiarbejdsliv.example to redirect to https://www.fysiarbejdsliv.example/dansk-selskab-for-fysioterapi-i-arbejdsliv. Is this possible?

  • How is the DNS for the domains set up? Do both domains currently point to the same server? How are the virtual hosts for the domains set up? Are they both ServerName or ServerAlias to the same DocumentRoot? – Stephen Ostermiller Apr 2 '19 at 10:17
  • “None of them are working for me” - please state specifically what the problem is. In what way are they not working? Do you get an error? Nothing happens? Do both domains redirect? Do you have other directives? – MrWhite Apr 2 '19 at 10:42
  • When I write “None of them are working for me” I mean there is no redirection at all: fysiarbejdsliv.example simply ends up on fysiarbejdsliv.example. As it is now both domains point to the same server. When the domains change only the url's change. I want ergonomi.example to remain as the frontpage when users use ergonomi.example and when users use fysiarbejdsliv.example I want to redirect to fysiarbejdsliv.example/… Best regards Pelle – Pelle P Apr 2 '19 at 20:28
  • I re-edited this for you to include the URL and code formatting, and generic domains. – dan Apr 3 '19 at 8:25
RewriteRule ^fysiarbejdsliv.example/(.*)$ https://www.fysiarbejdsliv.example/dansk-selskab-for-fysioterapi-i-arbejdsliv/$1 [L,R=301,QSA]

The RewriteRule pattern (first argument) matches against the URL-path only - this does not include the hostname. So the above directive will simply never match the request and will do nothing.

However, with this directive it looks like you are trying to do something very different to what you stated in the question. In your question you state that you want to redirect fysiarbejdsliv.example (no www subdomain?) to https://fysiarbejdsliv.example/dansk-selskab-for-fysioterapi-i-arbejdsliv only. But your directive has a capturing group in the pattern and you are using a backreference in the substitution? Is this just a bad case of copy/paste or a suggestion that you are really trying to do something else? This directive looks like you are trying to redirect fysiarbejdsliv.example/<anything> to https://www.fysiarbejdsliv.example/dansk-selskab-for-fysioterapi-i-arbejdsliv/<same-anything>?

Since both domains point to the same filesystem you will need an additional condition (RewriteCond directive) to check the Host header (ie. the domain name) before making the redirect.

For example, using mod_rewrite:

RewriteEngine On

RewriteCond %{HTTP_HOST} \bfysiarbejdsliv\.example [NC]
RewriteRule ^$ https://www.fysiarbejdsliv.example/dansk-selskab-for-fysioterapi-i-arbejdsliv [R=302,L]

The above redirects http[s]://[www.]fysiarbejdsliv\.example/ to https://www.fysiarbejdsliv.example/dansk-selskab-for-fysioterapi-i-arbejdsliv only.

This needs to go before any existing (or rather "conflicting") redirects/rewrites. If you have any mod_alias Redirect or RedirectMatch directives then these may need rewriting to use mod_rewrite instead.

Make sure you've cleared your browser cache before testing and only change the 302 (temporary) redirect to a 301 (permanent) when you are sure it's working OK to avoid caching issues.

| improve this answer | |

you can do the following
for external site

Redirect /path/to/old/file/old.html http://www.example.com/new/file/new.html 

for local site

Redirect /path/to/old/file/old.html /path/to/new/file/new.html

Link to the reference:-


| improve this answer | |
  • This doesn’t differentiate between the two domains, which both point to the same site, so both will be redirected. – MrWhite Apr 3 '19 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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