WordPress.org

Ready to get started?Download WordPress

Forums

Adding a 301 to WordPress .htaccess (8 posts)

  1. EddNCFC
    Member
    Posted 2 years ago #

    Hi,

    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]
    </IfModule>

    # END WordPress

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

  2. EddNCFC
    Member
    Posted 2 years ago #

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

  3. cjc1867
    Member
    Posted 2 years ago #

    I found it easier to use a plugin called Redirection.

  4. Johnb81
    Member
    Posted 2 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 2 years ago #

    After the # END WordPress line

    No. Always add additional rules BEFORE the WordPress block

  6. Johnb81
    Member
    Posted 2 years ago #

    Good point esmi. My bad there.

  7. EddNCFC
    Member
    Posted 2 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
    Member
    Posted 2 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

Tags