There are 2 ways to solve this. You can detect whether the user is in the Community context or not like Dave Humm suggests, or you can use design parameters to set the URL depending on where the Lightning Component is placed.
Detecting whether user is on the Community.
You can use the Site.getSiteId() method. This will have a value if the user is in a Community, but will be null if it is in Internal Salesforce.
Using Design Attributes
A better solution in my opinion would be using design attributes. This way, you can reuse the button even in more use cases, regardless of which user interface they are on.
Add a attribute to your component:
<aura:attribute name="url" type="String" />
Then add a design attribute to the design part of the component.
<design:attribute name="url" label="Redirect URL" required="true" />
This will map the the earlier created attribute and allows you to enter a value when you use the Lightning App Builder to put it on a page.
In your component you can read the value of this attribute when the button is clicked.