1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

XF 1.4 Redirects and route filters

Discussion in 'XenForo Questions and Support' started by shawn, Dec 30, 2014.

  1. shawn

    shawn Well-Known Member

    I recently moved the Xenforo installation from a /forum subdirectory into /. I have started to use route filters to pick up some of the old links, but I'm having problems with one specific example.

    If the old link went to www.site.com/forum/index.php?forums/$

    How do I get that to rewrite to www.site.com/forums/$1

    I'd be open to an nginx rewrite rule, too.
  2. Brogan

    Brogan XenForo Moderator Staff Member

    Rather than creating route filters, can't you achieve it with a single rule in .htaccess?

    RewriteRule ^forum/(.*)?$ /$1 [R=301,L]
  3. shawn

    shawn Well-Known Member

    Maybe, but I need to match the middle part with index.php?forums. I'm using "forum" for a route.
  4. shawn

    shawn Well-Known Member

    If you can't tell, I don't know Jack about reg expressions.
  5. shawn

    shawn Well-Known Member

    After 30 minutes of google-fu, I am now a regex master. Here's what I found out:

    mod_rewrite doesn't process anything after a question mark in the URL. So if you have mysite.com/forum/index.php?threads/this-is-my-thread.20 it will ignore all the important stuff. You have to have a rewrite conditional to look for the query string.

    I used a route filter to move /forums to /forum, and I never previously used the full friendly URLs, so this added some complexity to my problem. I solved it like this:

      RewriteCond %{QUERY_STRING} ^forums/(.*)$ [NC]
      RewriteRule ^forum/index\.php$ forum/%1 [NC,R=301,L]
    So, for anybody that was as clueless as this stuff as I, it says that if there's a query string (the part after the question mark) that includes the string "forums/", rewrite the url that was forum/index.php as forum/whateverwasinthesecondsetofparenthesisintheaboveconditional.

    I also had to add the following for the threads:

      RewriteCond %{QUERY_STRING} ^threads/(.*)$ [NC]
      RewriteRule ^forum/index\.php$ forum/threads/%1 [NC,R=301,L]
    If you want to get picky, I could have written them for countless other routes, but these two redirects, plus a generic catch-all for the /forum/index.php case solved all the significant ones.

      RewriteRule ^forum/index\.php$ forum [NC,R=301,L]
    If you were using full friendly URLs previously, you could solve the redirect issues like Brogan described above.

    This brief cheat sheet was immensely helpful in decoding the regular expressions in the rewrite rules:


    If somebody has a more elegant solution than the above, I'm all ears. But this series of rules worked for me.
    Last edited: Dec 31, 2014
  6. shawn

    shawn Well-Known Member

    I added this after the first two rewrite rules to capture all of the other random rewrites (index.php?misc/contact/, etc)

      RewriteCond %{QUERY_STRING} ^(.*)?$ [NC]
      RewriteRule ^forum/index\.php$ %1? [NC,R=302,L]

Share This Page