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
So, for example, if the user has a saved link to:
It would redirect to:
I’ve been pulling my hair out on this one and really appreciate any help.
I believe I can figure the rest out if someone can help me with 1 redirect.
As a start:
RewriteRule ^(forum|threads)/(.*)$ /community/$1/$2 [NC,L,R=301]
Thank you very much!
I had not tried that approach.
Could I also list the 3rd line like:
RewriteRule ^(forum|threads|pages|events|media|members)/(.*)$ /community/$1/$2/$3/$4/$5/$6 [NC,L,R=301]
The idea with the | is correct, but the /community/$1/$2/$3/$4/$5/$6 is not.
However many options you add, it still remains /community/$1/$2, since (forum|threads|pages|events|media|members) is still one atom, atom $1.
I think I’m slowly starting to get it.
Thanks for all the help!
Just some more explanation (in case you’re interested)
A RewriteRule is built as follows
RewriteRule from to flags
- from is a regular expression
- to is the URL that is loaded in case from matches the current URL
- flags are extra options for the rule
In the case of the rule above it would be
RewriteRule ^(forum|threads|pages|events|media|members)/(.*)$ /community/$1/$2 [NC,L,R=301]
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
Hope that helps