Lynnchun
Lynnchun

Reputation: 23

Need Help on htaccess Redirection

Need help to make htaccess. I use 2 domains: abc.domain1.com and domain2.com. the htaccess will be in domain2.com. i want to use it for vbulletin shorturl.

The idea are;

  1. redirect domain2.com/[numbers] (example= domain2.com/678) to abc.domain1.com/threads/[same numbers] (example = abc.domain1.com/threads/678)

  2. redirect domain2.com/a[numbers] (example= domain2.com/a1234) to abc.domain1.com/forums/[same numbers] (example = abc.domain1.com/forums/1234)

i tried for the case 1, is works with:

RedirectMatch ^/(.*) https://abc.domain1.com/threads/$1

but it doesn't work for case 2. i tried

RedirectMatch ^/f(.*) https://abc.domain1.com/forums/$1

but it gives me /threads/f[numbers]

Upvotes: 0

Views: 37

Answers (2)

MrWhite
MrWhite

Reputation: 45968

You could also resolve this by being more specific with the regex. If you only want to match numbers then you should be doing just that and matching just numbers. For example:

RedirectMatch ^/(\d*)$ https://abc.domain1.com/threads/$1
RedirectMatch ^/f(\d*)$ https://abc.domain1.com/forums/$1

The shorthand character class \d matches the digits 0-9 only. And the trailing end-of-string anchor ($) ensures no other characters follow in the URL-path.

As per your original directives, this also matches when no number at all is given. eg. / and /f only. Is that the intention?

Upvotes: 1

amphetamachine
amphetamachine

Reputation: 30621

The problem is that /f1234 matches ^/(.*) and it's being redirected to https://abc.domain1.com/threads/f1234.

Reverse the RedirectMatch lines, so that the more specific redirect is toward the top, and the "catch all" is at the very bottom:

RedirectMatch ^/f(.*) https://abc.domain1.com/forums/$1
RedirectMatch ^/(.*) https://abc.domain1.com/threads/$1

Upvotes: 2

Related Questions