1

I would like to setup a redirect using an .htaccess file which keeps part of the URL's path in the target URL. For example:

I have the domain example.com. I want example.com/social to redirect to twitter.com. BUT if someone types in example.com/social/Bob I want it to redirect to twitter.com/Bob, and would like it to work the same way for any value entered after the /social/ directory.

What might I add to an .htaccess file in order to do this?

1
  • I edited this to be less broad, and specific to doing this with an .htaccess file in order to make it on-topic.
    – dan
    Jul 26, 2014 at 0:52

1 Answer 1

2

This should do it.

RewriteEngine On
RewriteRule ^social(.*) https://twitter.com$1 [R=301,L]
3
  • 1
    Yeah, it needed the / removing before social but it works great after that :) Jul 26, 2014 at 10:38
  • 1
    A start of string anchor (ie. ^) is required on the RewriteRule pattern in order to avoid any ambiguity. ie. ^social(.*). Otherwise it will match URLs that simply contain the word "social".
    – MrWhite
    Jul 6, 2016 at 20:32
  • Thanks for the comment, I updated the answer to include your suggestion. Jul 7, 2016 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.