<p>The reason that you get that odd URL with the <a href="https://craftcms.com/docs/templating/redirect" rel="nofollow noreferrer"><code>{% redirect %}</code></a> tag is just because there's some whitespace at the beginning of the value returned from your <code>helpers.msuLink()</code> macro. Using the <a href="http://twig.sensiolabs.org/doc/filters/trim.html" rel="nofollow noreferrer"><code>|trim</code></a> filter (which removes whitespace from both ends of a string) fixes that:</p>

<pre><code>{% redirect helpers.msuLink(entry)|trim %}
</code></pre>

<p>If you prefer, you could also modify <a href="https://craftcms.stackexchange.com/questions/16504/setting-variables-for-nested-filters">your macro</a> so it returns the value without whitespace, by using the <a href="http://twig.sensiolabs.org/doc/tags/spaceless.html" rel="nofollow noreferrer"><code>{% spaceless %}</code></a> tag – then you don't need the <code>|trim</code> filter:</p>

<pre><code>{% macro msuLink(entry) %}
{% spaceless %}
    {{ 'https://reg.msu.edu/Courses/Request.aspx?SubjectCode=' ~ entry.deptCode ~ '&amp;CourseNumber=' ~ entry.courseNumber ~ '#Results' }}
{% endspaceless %}
{% endmacro %}
</code></pre>

<p>As for the conditional, I don't believe you want the <code>craft.entries</code> query there – just put the following at the top of your <code>courses/_entry.html</code> template (note that I'm also putting the <code>{% import %}</code> statement outside the conditional – it doesn't really matter but its cleaner and shouldn't impact performance):</p>

<pre><code>{% import 'partials/_helpers' as helpers %}

{% if entry.type == 'outsideCourse' %}
    {% redirect helpers.msuLink(entry)|trim %}
{% endif %}

{# Rest of template below #}
...
</code></pre>

<p>If you decide that having the <code>outsideCourse</code> URLs 404 instead of redirect makes the most sense (I believe it would, and is cleaner, in most cases), the easiest way to achieve that is to put this at the top of your entry template:</p>

<pre><code>{% if entry.type == 'outsideCourse' %}
    {% exit 404 %}
{% endif %}

{# Rest of the template below #}
...
</code></pre>

CC BY-SA 3.0
2016-08-26T22:48:53.333Z
You've come to the rescue again! That makes it work exactly how I want it to. I tested the 404 option as well, and that also works. Thanks.
Katie Fritz
CC BY-SA 3.0
2016-08-27T07:07:06.507Z
Awesome, @KatieFritz - I'm happy it worked out :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-25T09:01:53.490Z
2016-08-25T09:01:53.490Z
2017-04-13T12:51:00.200Z
Mats Mikkel Rummelhoff
<p>I have an entry type (<code>outsideCourse</code>) that will never need its own page. It only exists so I can manipulate it in various lists of required courses. Thanks to Mats Mikkel Rummelhoff, I have a macro to plug its field attributes into a search URL for another website. </p>

<p>Another entry type in the same section, <code>safsCourse</code>, DOES have its own template URL scheme.</p>

<p>I don't want to have a mostly blank page show up if someone happens to go to the entry page for one of the <code>outsideCourse</code> entries (example: <code>localhost/craftcms/courses/ecological-economics</code>). </p>

<p>The options I can think of:</p>

<ol>
<li>Somehow eliminate slugs or URLs for the <code>outsideCourse</code> entry type, so their URLs go to the 404 page</li>
<li><code>.htaccess</code> redirects</li>
<li>A separate template just for this entry type that redirects either to my preferred destination (the <a href="http://msu.edu" rel="nofollow">http://msu.edu</a> link generated by my macro) or the index page for this section (<code>localhost/craftcms/courses</code>). </li>
<li>One template for both entry types, with a conditional redirect. </li>
</ol>

<p>I've been working on Option 4. I've tried adding the following to the very beginning of my <code>courses/_entry.html</code> template, before <code>{% extends '_layout' %}</code>:</p>

<pre><code>{% if craft.entries.section('courses').type('outsideCourse') %}
     {% import 'partials/_helpers' as helpers %}
        {% redirect helpers.msuLink(entry) %}
 {% else %} 
</code></pre>

<p>It tries to go to <code>http://localhost/craftcms/%20%20%20%20https://reg.msu.edu/Courses/Request.aspx?SubjectCode=EEP&amp;amp;CourseNumber=260#Results</code> and gives me a 403 Forbidden error.</p>

<p>If I replace <code>helpers.msuLink(entry)</code> with <code>"http://msu.edu"</code>, it redirects but ignores the conditional. </p>

<p><strong>So here's what I'm wondering:</strong></p>

<ul>
<li>Why isn't the conditional working?</li>
<li>How can I add an absolute link to a redirect without just writing a string?</li>
<li>Should I be going about this in a different way altogether?</li>
</ul>

<templating><routes><entry-type><redirect>
Redirect entry type to another domain
# Redirect entry type to another domain

(Asked by: username_0 on 2016-08-25)

I have an entry type (outsideCourse) that will never need its own page. It only exists so I can manipulate it in various lists of required courses. Thanks to username_1, I have a macro to plug its field attributes into a search URL for another website. 
Another entry type in the same section, safsCourse, DOES have its own template URL scheme.
I don't want to have a mostly blank page show up if someone happens to go to the entry page for one of the outsideCourse entries (example: localhost/craftcms/courses/ecological-economics). 
The options I can think of:

Somehow eliminate slugs or URLs for the outsideCourse entry type, so their URLs go to the 404 page
.htaccess redirects
A separate template just for this entry type that redirects either to my preferred destination (the http://msu.edu link generated by my macro) or the index page for this section (localhost/craftcms/courses). 
One template for both entry types, with a conditional redirect. 

I've been working on Option 4. I've tried adding the following to the very beginning of my courses/_entry.html template, before {% extends '_layout' %}:
{% if craft.entries.section('courses').type('outsideCourse') %}
     {% import 'partials/_helpers' as helpers %}
        {% redirect helpers.msuLink(entry) %}
 {% else %} 

It tries to go to http://localhost/craftcms/%20%20%20%20https://reg.msu.edu/Courses/Request.aspx?SubjectCode=EEP&CourseNumber=260#Results and gives me a 403 Forbidden error.
If I replace helpers.msuLink(entry) with "http://msu.edu", it redirects but ignores the conditional. 
So here's what I'm wondering:

Why isn't the conditional working?
How can I add an absolute link to a redirect without just writing a string?
Should I be going about this in a different way altogether?

**Tags:** templating, routes, entry-type, redirect


---

## 1 answers

### Answer by username_1 on 2016-08-25. Score: 4 | Accepted answer

The reason that you get that odd URL with the {% redirect %} tag is just because there's some whitespace at the beginning of the value returned from your helpers.msuLink() macro. Using the |trim filter (which removes whitespace from both ends of a string) fixes that:
{% redirect helpers.msuLink(entry)|trim %}

If you prefer, you could also modify your macro so it returns the value without whitespace, by using the {% spaceless %} tag – then you don't need the |trim filter:
{% macro msuLink(entry) %}
{% spaceless %}
    {{ 'https://reg.msu.edu/Courses/Request.aspx?SubjectCode=' ~ entry.deptCode ~ '&CourseNumber=' ~ entry.courseNumber ~ '#Results' }}
{% endspaceless %}
{% endmacro %}

As for the conditional, I don't believe you want the craft.entries query there – just put the following at the top of your courses/_entry.html template (note that I'm also putting the {% import %} statement outside the conditional – it doesn't really matter but its cleaner and shouldn't impact performance):
{% import 'partials/_helpers' as helpers %}

{% if entry.type == 'outsideCourse' %}
    {% redirect helpers.msuLink(entry)|trim %}
{% endif %}

{# Rest of template below #}
...

If you decide that having the outsideCourse URLs 404 instead of redirect makes the most sense (I believe it would, and is cleaner, in most cases), the easiest way to achieve that is to put this at the top of your entry template:
{% if entry.type == 'outsideCourse' %}
    {% exit 404 %}
{% endif %}

{# Rest of the template below #}
...

> **Comments:**
> **username_0**: You've come to the rescue again! That makes it work exactly how I want it to. I tested the 404 option as well, and that also works. Thanks.  
> **username_1**: Awesome, @KatieFritz - I'm happy it worked out :)  

---

