Search code examples
wordpressapache.htaccess

Redirecting specific urls to subfolder on same domain


I have moved my phpBB board from dommain.com to domain.com/forum. I have launched a Wordpress website on domain.com.

As a consequence, all links to topics on my board end up on a 404 page of my Wordpress website on domain.com

I would like to redirect links to topics on my board.

For example:

Redirect: example.com/viewtopic.php?f=17&t=35

To: example.com/forum/viewtopic.php?f=17&t=35

So only /forum should be added.

Is changing the .htaccess file of my WordPress website the best way to do this? And if so, what would be the correct code?

Of are there better ways (faster/better for SEO) that I don't know of.


Solution

  • Is changing the htaccess file of my Wordpress website the best way to do this?

    Implementing this in the root .htaccess file is a reasonable way to do this, unless you have access to the main server config. Alternatively, you could perform this redirect in PHP (in WordPress), but that puts the burden on your WordPress site.

    For example, at the top of your root .htaccess file, before the existing WordPress directives (ie. before # BEGIN WordPress) try the following:

    # Redirect old forum topics to "/forum" subdirectory
    RewriteRule ^viewtopic\.php$ /forum/$0 [R=301,L]
    

    Any query string is passed through by default. So, a request for /viewtopic.php?<something> is redirected to /forum/viewtopic.php?<something>.

    The $0 backreference contains the URL-path matched by the entire RewriteRule pattern. ie. "viewtopic.php" (this simply saves repetition).

    NB: You should test first with a 302 (temporary) redirect to avoid potential caching issues.