Mobile app version of vmapp.org
Login or Join
Courtney195

: 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

@Courtney195

Posted in: #301Redirect #Htaccess #Redirects #Wordpress

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

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Courtney195

2 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

The important thing is to place your external 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 stuff 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 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 (.*) example.com/ [R=301,L]


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

10% popularity Vote Up Vote Down


 

@Angela700

# 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$ domain.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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme