0

Multiple alias domains pointing to the same cPanel account are sharing the same .htaccess file. I need a domain-specific rule that redirects all traffic from aliasdomain.com to https://www.domain1.com only if:

  • HTTP_HOST is currently aliasdomain.com (with or without WWW and http/https)

  • exclude folder /medias/ - it must still be accessible from aliasdomain.com

  • exclude all *.jpg, *.jpeg, *.png and *.gif files (even if the files are in root of aliasdomain.com)

  • always redirect to https://www.domain1.com root, no wildcard redirect and no redirect to non-ssl http

  • domain1.com needs to be excluded so if the same .htaccess file is on domain1.com it won't interfere.

I tried to adapt various code snippets found on StackOverflow but failed horribly getting it to work :( I asked a question there but mods told me to ask on ServerFault instead so here i am... Help would be really appreciated!

MrWhite
  • 12,647
  • 4
  • 29
  • 41
comdex420
  • 3
  • 2

1 Answers1

1

Try the following:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?aliasdomain\.com [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g|png|gif)$ [NC]
RewriteRule !^medias https://www.domain1.com/ [R=302,L]

The RewriteRule pattern ensures that the rule is only processed when not requesting URLs that start /medias. (The ! prefix negates the regex in Apache mod_rewrite.)

The first RewriteCond directive checks that the host being requested is aliasdomain.com only (with or without the www subdomain). (So, domain1.com is naturally excluded.)

The second RewriteCond directive excludes requests for image files.

Change the 302 (temporary) redirect to 301 (permanent) if this is intended to be permanent and only once you are sure it's working OK (since 301s are cached hard by the browser).

Clear your browser cache before testing, as any previous/erroneous 301s are likely to have been cached. (Or test with browser tools open and cache disabled.)

I asked a question there but mods told me to ask on serverfault instead

To be honest, this question is probably better suited to the Pro Webmasters stack, rather than ServerFault! (Although this isn't necessarily off-topic on StatckOverflow, an example of the code you have tried is mandatory.)

MrWhite
  • 12,647
  • 4
  • 29
  • 41