1

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

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>
  • 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

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.