0

I want to redirect a URL from a page that is no longer active. The URL https://tchibo.academyofsports.promo/ as well as all subpages should be redirected to the URL http://www.academyofsports.de/. This is what I have so far (.htaccess):

#Redirects

RewriteEngine On
RewriteRule https://tchibo.academyofsports.promo/ http://www.academyofsports.de/ [L,R=301]

Unfortunately it does not work in this way. What am I doing wrong?

Codehan25
  • 2,704
  • 10
  • 47
  • 94

1 Answers1

1

This one probably is what you are looking for:

RewriteEngine On
RewriteCondition %{HTTP_HOST} ^tchibo\.academyofsports\.promo$ [NC]
RewriteRule ^/?(.*)$ http://www.academyofsports.de/$1 [L,R=301]

Reason is that a RewriteRule only operates on the path component of the requested URL, it cannot see the host name. The above rule should work likewise in the http servers host configuration and dynamic configuration files.

If you are not interested in the path that got requested originally but want to redirect everyone to the entry point, then you can simplify the above to:

RewriteEngine On
RewriteCondition %{HTTP_HOST} ^tchibo\.academyofsports\.promo$ [NC]
RewriteRule ^ http://www.academyofsports.de/ [L,R=301]

And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).


Reacting to your comment below I want to add this:

To implement specific mappings you can also add exception rules:

RewriteEngine On

RewriteCondition %{HTTP_HOST} ^tchibo\.academyofsports\.promo$ [NC]
RewriteRule ^/?agb\.html$ http://www.academyofsports.de/Datenschutz [L,R=301]

RewriteCondition %{HTTP_HOST} ^tchibo\.academyofsports\.promo$ [NC]
RewriteRule ^/?(.*)$ http://www.academyofsports.de/$1 [L,R=301]

The issue is that you have to repeat the condition for each single rule due to the internal logic of the rewrite module. There are one or two crude hacks to get around this, but things get hard to maintain very fast that way. If you need to specify multiple such exception rules a RewriteMap is the way to go.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Works like a charm, thank you! But what about if I want to redirect https://tchibo.academyofsports.promo/agb.html to http://www.academyofsports.de/Datenschutz? – Codehan25 May 05 '17 at 07:11
  • Great to hear that I could help :-) I added some hint about such exception rules to the question. Have fun! – arkascha May 05 '17 at 07:27
  • Hmm, now I get "Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request." – Codehan25 May 05 '17 at 07:43
  • Consider the hint you get: you should _always_ look into your http servers error log file if you experience a http status 500. – arkascha May 05 '17 at 08:05
  • I think there is something wrong with the regular expression ^?/agb\.html$ because if I remove it everything works fine. Also tried ^/?agb\.html$.. – Codehan25 May 05 '17 at 08:10
  • Ah, sorry, obviously the question mark has to come after the leading slash, a typo on my side. I changed it in the answer. – arkascha May 05 '17 at 08:12
  • And "doesn't work" means what? What does your error log file say what the exact issue is? – arkascha May 05 '17 at 09:26
  • It says: 5.158.158.228 - - [05/May/2017:11:38:38 +0200] "GET /agb.html HTTP/1.1" 404 1363 tchibo.academyofsports.promo "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36" "-" – Codehan25 May 05 '17 at 09:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143481/discussion-between-arkascha-and-codehan25). – arkascha May 05 '17 at 09:40