1

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
  • It is where it should be - before the file level stuff. – closetnoc Feb 8 '16 at 4:26
  • Pay attention to both answers from Mike and w3dk. – closetnoc Feb 8 '16 at 18:18
2
# 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.

  • Much cleaner! Cheers!! – closetnoc Feb 8 '16 at 18:17
  • 1
    "the rewrite rules should be inside the IfModule block" - This isn't strictly correct. You shouldn't modify the code inside the # BEGIN WordPress block (as you have done in the example) as this is maintained by WordPress itself (at least it tries to if you let it). Whether you include your directives inside an <IfModule> container or not is dependent on whether your site should function without mod_rewrite - and the answer to that is probably not. – MrWhite Mar 26 at 22:47
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.