Skip to content

Commit

Permalink
fix: #8549 send 308 Permanent Redirect on topic/category shortlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Aug 6, 2020
1 parent def16f9 commit 68f8d6e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/controllers/category.js
Expand Up @@ -39,7 +39,7 @@ categoryController.get = async function (req, res, next) {
}

if (!res.locals.isAPI && (!req.params.slug || categoryFields.slug !== cid + '/' + req.params.slug) && (categoryFields.slug && categoryFields.slug !== cid + '/')) {
return helpers.redirect(res, '/category/' + categoryFields.slug);
return helpers.redirect(res, '/category/' + categoryFields.slug, true);
}

const topicCount = categoryFields.topic_count;
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/helpers.js
Expand Up @@ -151,11 +151,11 @@ helpers.notAllowed = async function (req, res, error) {
}
};

helpers.redirect = function (res, url) {
helpers.redirect = function (res, url, permanent) {
if (res.locals.isAPI) {
res.set('X-Redirect', encodeURI(url)).status(200).json(url);
} else {
res.redirect(nconf.get('relative_path') + encodeURI(url));
res.redirect(permanent ? 308 : 307, nconf.get('relative_path') + encodeURI(url));
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/topics.js
Expand Up @@ -44,7 +44,7 @@ topicsController.get = async function getTopic(req, res, callback) {
}

if (!res.locals.isAPI && (!req.params.slug || topicData.slug !== tid + '/' + req.params.slug) && (topicData.slug && topicData.slug !== tid + '/')) {
return helpers.redirect(res, '/topic/' + topicData.slug + (postIndex ? '/' + postIndex : '') + (currentPage > 1 ? '?page=' + currentPage : ''));
return helpers.redirect(res, '/topic/' + topicData.slug + (postIndex ? '/' + postIndex : '') + (currentPage > 1 ? '?page=' + currentPage : ''), true);
}

if (postIndex === 'unread') {
Expand Down

0 comments on commit 68f8d6e

Please sign in to comment.