Where to place 301 redirects in the .htaccess file on a WordPress site
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
# 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.
Comments
Post a Comment