Where to place 301 redirects in the .htaccess file on a WordPress site

Where to place 301 redirects in the .htaccess file on a WordPress site - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the Where to place 301 redirects in the .htaccess file on a WordPress site error on your web browser. Problem :


I edited my .htaccess file earlier to redirect from www version to non-www, and it is working correctly. However, now I have some page redirects I need to do. I am looking at the .htaccess file, and I am not sure where to place the actual redirect code - I have a WordPress site. Does it go at the end, or in the area where it says WordPress? Here is my .htaccess file:



# Use PHP5.4 as default
# Changed PHP handler from application/x-httpd-php54 to application/x-httpd-phpbeta
# on Thu Dec 17 16:50:26 MST 2015.
AddHandler application/x-httpd-phpbeta .php

RewriteEngine on
RewriteCond %HTTP_HOST ^www.example.com$
RewriteRule ^/?$ "http://example.com/" [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^index.php$ - [L]
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Solution :

# Use PHP5.4 as default
# Changed PHP handler from application/x-httpd-php54 to application/x-httpd-phpbeta on Thu Dec 17 16:50:26 MST 2015.
AddHandler application/x-httpd-phpbeta .php

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Insert redirect based rewrites here.
# Example: RewriteRule ^redirectme$ http://example.com/redirectme [R=301,L]
#
RewriteCond %HTTP_HOST ^www.example.com$
RewriteRule ^/?$ "http://example.com/" [R=301,L]
RewriteRule ^index.php$ - [L]
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress


I modified your .htaccess file a bit. First, the rewrite rules should be inside the IfModule block, because if the server administrator removed mod_rewrite from Apache later on, then your code won't break the system, but will instead be ignored.



Also, check my code to see the best spot to put in redirects. I suggested there to minimize latency. The rules are checked in order from top to bottom.



The important thing is to place your external (mod_rewrite[*1]) redirects before the WordPress internal rewrites (as Mike suggests in his answer).



The WordPress rewrites are a "catch all" and rewrite everything. So if you place any rewrites/redirects after the WordPress block they will simply be ignored.



RewriteCond %HTTP_HOST ^www.example.com$
RewriteRule ^/?$ "http://example.com/" [R=301,L]


(Apart from the plethora of unnecessary escapes in the substitution), this is not complete. This only redirects requests to the site root. Presumably you also want to redirect www.example.com/<whatever> to example.com/<whatever>? In which case you need something like this instead (in .htaccess):



RewriteCond %HTTP_HOST ^www.example.com$
RewriteRule (.*) http://example.com/$1 [R=301,L]


(Are you also setting your preferred domain in the WordPress config?)






[*1] Note that if you were using mod_alias Redirect (or RedirectMatch) directives for your redirects then it wouldn't strictly matter where you placed your redirects in relation to the WordPress front-controller (that uses mod_rewrite, ie. RewriteCond and RewriteRule directives). This is because the mod_alias (Redirect / RedirectMatch) directives will always be processed (after mod_rewrite / RewriteRule), regardless of their position in the config file.


We hope that this article has helped you resolve the htaccess, redirects, wordpress error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

How do I redirect traffic only if being accessed from a specific port?

DNS entry to redirect constant url to changing directory name

How to redirect to any domain [duplicate]