2

I'm trying to rewrite a filename based on the server's domain.

This code below is wrong / not working, but illustrates the desired effect.

<If "req('Host') != '*.mydevserver.com'">
  RewriteRule "^/robots\.txt$"  "robots-staging.txt"    [R]
</If>

Desired effect is this htaccess will point to a different robots.txt depending on if it's on staging (wildcard subdomain) or not.

Example usage:

http://client1.devsite.com/robots.txt rewritten to
http://client1.devsite.com/robots-staging.txt

http://client2.devsite.com/robots.txt rewritten to
http://client2.devsite.com/robots-staging.txt

https://client.com/robots.txt not rewritten
https://myclient.com/robots.txt

unor
  • 246
  • 2
  • 19
Jay
  • 157
  • 1
  • 7

2 Answers2

3

You can use a more dynamic approach:

RewriteRule ^robots\.txt$ robots/%{HTTP_HOST}.txt [NS]

And place your robots.txt files like follows:

  • robots/domain.tld.txt
  • robots/sub.domain.tld.txt

I came across this solution due to some multi website projects based on TYPO3 CMS and Neos CMS.

Andy
  • 31
  • 1
2

I found my own answer. This works as desired.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^(.*)?mydevserver(\.com)
    RewriteRule ^robots\.txt$ robots-staging.txt [NS]
</IfModule>
Jay
  • 157
  • 1
  • 7
  • 1
    You could simplify your _CondPattern (`^(.*)?mydevserver(\.com)`) to `mydevserver\.com` (no need for the optional capturing sub patterns and anchors if you are just checking for "mydevserver.com"). You should probably include the `L` flag in case you add more directives. – MrWhite Nov 18 '17 at 16:01