I am moving my Xenforo installation from the document root to /community and I don’t want to lose any links.
I’ve tried many different ways to do this using .htaccess and mod_rewrite but it seems I can’t figure it out.
what I need is anything going directly to the document root (/) to be left alone. For example, http://www.xxx.com/index.php will not forward.
Anything going to /forum to be forwarded to /community/forum
Anything going to /threads will forward to /community/threads
Anything going to /pages will forward to /community/pages
Anything going to /events will go to /community/events
Anything going to /media will go to /community/media
Anything going to /members will go to /community/members
What we match here is first either forum, or threads, or pages, or …(etc), followed by a forward slash, followed by whatever (a . in a regular expression means “any character”, and * means “zero or more”, so .* means “zero or more of whatever character”).
The parentheses in the from part tell Apache that it should store whatever matches within those parentheses as a variable. In this case whatever matches (forum|threads|pages|events|media|members) is stored in variable $1, and whatever matches (.*) is stored in variable $2. The part between the parentheses (including the parentheses themselves) are called atoms.
(you don’t get to pick the variable names BTYW; Apache uses $1 … $9, if you need more than 9 atoms… well, you can’t I’ve never needed that BTW. I think I most I ever used is like 5 or maybe 6)
Then we get to the to part, we see the $1 and $2 in there, and we tell Apache to load the URL /community/<whatever we matched in the first pair of parentheses>/<whatever we matched in the second pair of parentheses>
As for the flags, there are three of them in this rule
NC - No Case – match case insensitive, so the rule will also match /Forum, /Threads, /ThReAds, etc
L - Last – This is kind of a hard one to explain. Short explanation is that it’s faster if you use this flag on every RewriteRule most of the time (like 99,9% of the time)
R=301 – Redirect with status 301 – tells Apache that it should tell the browser to redirect. Normally Apache will internally load the to URL, while it keeps showing the original URL in the browser, but in this case you actually want the URL in the browser to change, so you add the R=301