1

Can someone help me with .htaccess file?

I changed my domain name from lets say olddomain.com to newdomain.com without building a completely different site.

I just went in and changed the URL and fixed a few things in cPanel or something and then parked the domain and went on with my life. It was a new site so I didn't think it was important to redirect as I wasnt getting any traffic anyway.

Now my super important blog post is ranking and but on my old URL and there is absolutely no way to 301 redirect anything!

I tried plugins its basically only giving me the option to 301 redirect from my new URL.

I didn't do my homework I know.

This is what I did:

  • Changed urls directly on the same wordpress site
  • submitted a new sitemap on google search console
  • a few blog posts didn't get index because they obviously see it as duplicate content
  • parked the old domain

2 months later:

  • still ranking on my old blog post
  • still not indexed on my new one
  • removed the parked domain to see if it makes a difference
  • removed the site map on the old domain
  • asked google to request indexing

It's not indexed, now the blog post is not coming to my site obviously since I removed the parked domain.

I contacted the siteground customer service they told me I had to edit my .htaccess file. Through their tutorial it isn't showing the exact same lines of code.

this is siteground's tutorial : To ensure your new primary domain name is used, first park the previous primary one as shown in this tutorial and then redirect the old domain name to your new one by inserting this code to the .htaccess file in your public_html folder (you can modify/create that file via File Manager in Site Tools):

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

this is MY .htaccess code:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# END HTTPS
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Any advice would be greatly appreciated. I definitely learned a lesson here.

I will obviously repark the domain if I can fix this .htaccess file.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
ss5592
  • 9
  • 1
  • 3
  • Does this answer your question? [Redirect all request from old domain to new domain](https://stackoverflow.com/questions/19357085/redirect-all-request-from-old-domain-to-new-domain) – Walf May 31 '22 at 03:58

1 Answers1

1

There doesn't seem to be anything wrong with your current .htaccess file, you just need to implement an external 301 redirect from the old domain to the new domain - at the very top of your .htaccess file (before the # BEGIN WordPress comment marker).

However, assuming both the main and "parked" domains are pointing to the same site (same area on the filesystem) then you can't simply redirect as the Siteground tutorial would appear to suggest, as that will naturally create a redirect loop - since it unconditionally redirects everything to the new domain, including requests for the new domain! You need to check that the old domain (or not the new domain) was requested before redirecting.

Add just the following at the top of the .htaccess file:

RewriteCond %{HTTP_HOST} !^www\.newdomain\.com$
RewriteRule (.*) https://www.newdomain.com/$1 [R=301,L]

I assume the canonical scheme and hostname is HTTPS + www.newdomain.com.

The above states for all requests (.*) where the requested Host header is not (! prefix) www.newdomain.com then redirect to https://www.newdomain.com/<urlpath> - on the same URL-path as requested.

You need to make sure that WordPress is configured for the new domain and all internal links are constructed using the new domain.

You should first test this with a 302 (temporary) redirect before changing to a 301 (permanent) redirect to avoid potential caching issues.

You do not need to repeat the RewriteEngine On directive. The RewriteEngine directive that appears later in the file is sufficient.

MrWhite
  • 43,179
  • 8
  • 60
  • 84