XF 2.2 Redirects with htaccess for Discourse URLs

masmo

Member
Hi, since the redirect script is not available for Discourse, I would like to try to do something like this.

Can someone give me a little help? šŸ„¹

I would like to convert only thread URLs from
https://name.site.com/t/thread-title-to-ignore/1
to
https://name.site.com/forums/threads/1

And
https://name.site.com/t/thread-title-to-ignore/1?page=2
to
https://name.site.com/forums/threads/1/page-2

I don't understand how to adapt this to Discourse:
RewriteCond %{QUERY_STRING} (^|&)t=([0-9]+)(&|$) [NC] RewriteRule ^viewtopic\.php$ /threads/%2/? [L,R=301,NC]

Thanks
 
Solution
Just two considerations after one week of use.

In order not to lose all the external links it is better to also add the forwarding rule:
/t/some-thread-123/1/7 to /threads/1

In summary, with the following rules it will work like this:
/t/some-thread-123/1 to /threads/1
/t/some-thread-123/1/7 to /threads/1
/t/some-thread-123/1?page=2 (used by Google and others bots) to /threads/1/page-2

Apache config:
#    Jeremy's Discourse URL converter
    RewriteCond %{QUERY_STRING} (?:^|&)page=(\d+)(?:$|&) [NC]
    RewriteRule ^t/[^/]+/(\d+)/?$ /threads/$1/page-%1? [L,NC,R=301]
    RewriteRule ^t/[^/]+/(\d+)+/(\d+)/?$ /threads/$1/? [L,NC,R=301]
    RewriteRule ^t/[^/]+/(\d+)/?$ /threads/$1/? [L,NC,R=301]
I haven't tested this and I'm very rusty with the intricacies of Apache/ModRewrite in general, but these rewrite rules may work:

Code:
RewriteRule ^/t/[a-z0-9-]+/(\d+)$ /forums/threads/$1/ [L,NC,R=301]
RewriteRule ^/t/[a-z0-9-]+/(\d+)/(\d+)$ /forums/threads/$1/page-$2 [L,NC,R=301]

The first should redirect /t/some-thread-123/1 to /forums/threads/1. The second should redirect /t/some-thread-123/1/2 (which I think is the correct Discourse page URL format?) to /forums/threads/1/page-2.
 
The second should redirect /t/some-thread-123/1/2 (which I think is the correct Discourse page URL format?
This url is for posts but it is unusable because the numbers don't match.

Pages instead have this url
/t/some-thread-123/1?page=2
To be more specific, this is the url that google indexed, it's a version of the site that Discourse builds without Java.
 
Last edited:
Oh, maybe something like this for those ones then:

Code:
RewriteCond %{QUERY_STRING} ^page=(\d+)$ [NC]
RewriteRule ^/t/[a-z0-9-]+/(\d+)$ /forums/threads/$1/page-%1? [L,NC,R=301]

I think this rule would need to go before the other one.
 
Unfortunately neither of them work, the url is not changed.
To be sure, I thought I was wrong by modifying the .htaccess file, I tested the one for phpBB and it works.
Thanks anyway, I'll do more tests.
 
I did some brief testing, placing them in the .htaccess file of the document root (not in forums/), and they appeared to redirect as expected. The matching was a bit strict though. It would only accept alphanumerics and hyphens in the thread title, no trailing slashes, and page must be the only thing present in the query string. Here is a different variation which relaxes those rules a bit, which also worked in my testing:

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{QUERY_STRING} (?:^|&)page=(\d+)(?:$|&) [NC]
    RewriteRule ^/t/[^/]+/(\d+)/?$ /forums/threads/$1/page-%1? [L,NC,R=301]

    RewriteRule ^/t/[^/]+/(\d+)/?$ /forums/threads/$1/? [L,NC,R=301]
</IfModule>
 
The problem was the / before the t
So it's perfect:
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{QUERY_STRING} (?:^|&)page=(\d+)(?:$|&) [NC]
    RewriteRule ^t/[^/]+/(\d+)/?$ /forums/threads/$1/page-%1? [L,NC,R=301]

    RewriteRule ^t/[^/]+/(\d+)/?$ /forums/threads/$1/? [L,NC,R=301]
</IfModule>

Thanks @Jeremy P , without your help I never would have succeeded!
 
Strange, but again I won't pretend to know the ins and outs of mod_rewrite or that I spent much time reading through the documentation before cobbling together those rules :) I'm glad you got it working, hopefully others may find it useful as well.
 
Just two considerations after one week of use.

In order not to lose all the external links it is better to also add the forwarding rule:
/t/some-thread-123/1/7 to /threads/1

In summary, with the following rules it will work like this:
/t/some-thread-123/1 to /threads/1
/t/some-thread-123/1/7 to /threads/1
/t/some-thread-123/1?page=2 (used by Google and others bots) to /threads/1/page-2

Apache config:
#    Jeremy's Discourse URL converter
    RewriteCond %{QUERY_STRING} (?:^|&)page=(\d+)(?:$|&) [NC]
    RewriteRule ^t/[^/]+/(\d+)/?$ /threads/$1/page-%1? [L,NC,R=301]
    RewriteRule ^t/[^/]+/(\d+)+/(\d+)/?$ /threads/$1/? [L,NC,R=301]
    RewriteRule ^t/[^/]+/(\d+)/?$ /threads/$1/? [L,NC,R=301]
 
Solution
Top Bottom