Adding a 301 to WordPress .htaccess (8 posts)

  1. EddNCFC
    Posted 4 years ago #


    I need to add in a 301 redirect to the standard WordPress .htaccess file as I moved my site from http://www.olddomain.com to http://www.newdomain.com.

    This is my current .htaccess file located in /news/

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /news/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /news/index.php [L]

    # END WordPress

    I've tried many things but none of them work, any ideas?

  2. EddNCFC
    Posted 4 years ago #

    I've looked all over this site and this question has been asked many times but never answered.

  3. cjc1867
    Posted 4 years ago #

    I found it easier to use a plugin called Redirection.

  4. Johnb81
    Posted 4 years ago #

    After the # END WordPress line you can simply do the following:

    Options +FollowSymlinks
    RewriteEngine on
    rewritecond %{http_host} ^domain.com [nc]
    rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

    Replace domain.com with your website. This ensures that all requests sent to the old website should be redirected to the new domain.

    P.S. this code should go in the htaccess of the old website and not the new one.

  5. esmi
    Forum Moderator
    Posted 4 years ago #

    After the # END WordPress line

    No. Always add additional rules BEFORE the WordPress block

  6. Johnb81
    Posted 4 years ago #

    Good point esmi. My bad there.

  7. EddNCFC
    Posted 4 years ago #

    Thank you for your feedback.

    The only issue with that solution is that I've pointed my new domain to the old domains location.

    In the root directory I have a .htaccess file with this:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^(www\.newdomain\.com)?$
    RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

    This works great.

    It's just the WordPress URLs that aren't getting redirected because they go via the sub folder /news/ which is where the WordPress .htaccess file resides to rewrite the nice URLs.

  8. Johnb81
    Posted 4 years ago #

    You can write further URL rewrite rules to redirect old posts to the new posts, but if you do not have much posts, I would recommend you to use the "Permanent Redirect" rule in the htaccess file and make a rule for each URL.

Topic Closed

This topic has been closed to new replies.

About this Topic