• Hi all,

    Is it possible to redirect URLs only in a specific directory to another domain?

    For example: mysite.com/event/event-name when clicked on redirects to events.mysite.com/event/event-name?

    In other words, I would need something that would say: if a URL starts with the directory “events”, replace “mysite.com” with “events.mysite.com” and keep the rest of the URL intact.

    Thanks for any help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • There at at least two ways of doing this. either by changing the .htaccess file (or creating one) in the folder you want to redirect from and adding re-write rules. See https://codex.www.ads-software.com/htaccess and https://code.tutsplus.com/tutorials/the-ultimate-guide-to-htaccess-files–net-4757

    Another approach is to use the WordPress rewrite API(see https://codex.www.ads-software.com/Rewrite_API).

    In my experience both have their drawbacks. Modifying the .htaccess file can get complicated quickly because at any point in the folder structure where a .htaccess file exists then the rules in the file will get applied. This can make debugging your re-write rules tricky to say the least.

    When I have used the ReWrite API you usually have to re-save the permalinks from the front end to make them work. From the page on add_rewrite_rule

    IMPORTANT: Do not forget to flush and regenerate the rewrite rules database after modifying rules. From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.

    However using the Rewite API I have found it is easier to debug what is happening when the rewrite rules are not working as expected.

    Yes. You need to add a 301 redirect in the htaccess of your parent directory of your website. See below for a sample based on the info you provided.
    Redirect 301 /event/event-name https://events.mysite.com/event/event-name

    Hope this helps. Let us know if you have any further questions. ??

    Thread Starter marylhurst

    (@marylhurst)

    Hi Davood,

    Thanks!

    Is there any way to take that a step further and do sort of a wildcard for the pages within the directory?

    For example, any page in the “event” directory is changed?

    E.g., /event/* is redirected to https://events.mysite.com/event/*

    Or some kind of script that would change it on the page to just swap the root domain on a link if a link ever has the /event/ directory?

    Thank you!

    This would work:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^/event/event-name(.*)$ https://events.mysite.com/event/event-name$1 [R=301,L]
    </IfModule>

    Thank you @markrh for filling in the gap for me. I actually did not know the htaccess code for that and was going to say you will need some custom htaccess or php code to accomplish it.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Redirecting only some pages on one domain to another domain’ is closed to new replies.