How to Make Custom Rewrite Rules Work with WordPress | HostGator Support
  1. Knowledge Base
  2. >
  3. Results
  4. >
  5. How to Make Custom Rewrite Rules Work with WordPress

How to Make Custom Rewrite Rules Work with WordPress

While our agents will do their best to offer as much support as possible, there are some issues that fall beyond our scope of support.  This includes custom coding, web design and third party script design. 

If you are using custom coding, please contact your web developer for further assistance.

For additional information on custom scripting and coding, please read:

You may notice that any custom rewrite rules you make for specific folder paths under your site's domain will simply produce 404 errors on your WordPress blog.

To fix this, try the following:

  1. Open your .htaccess file. The code for WordPress will look like this:
        # BEGIN WordPress
        <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
        </IfModule>
        # END WordPress
        
  2. List paths which should not obey WordPress rewrites in the RewriteCond %{REQUEST_URI} !^/(path1|path2|path3) [NC]. Here is the fix in action:
        # BEGIN WordPress
        <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
    
        RewriteCond %{REQUEST_URI} !^/(abc|xxx|events/go-to-town) [NC]
    
        RewriteRule . /index.php [L]
    
        RewriteRule ^abc/$ "http://wordpress.com/" [R=301,L]
        RewriteRule ^xxx/$ "https://support.hostgator.com/" [R=301,L]
        RewriteRule ^events/go-to-town/$ "http://secure.hostgator.com/~affiliat/cgi-bin/affiliates/clickthru.cgi?id=whatever" [R=301,L]
    
        </IfModule>
        # END WordPres
    
     
    Note: This code works for WordPress blogs hosted on their own domain (primary, addon, subdomain); however, the code must be changed to work with a blog installed in a subfolder without its own domain.

    To make this work with a www.mydomain.com/blog type address, you just need to tweak one line:
    RewriteCond %{REQUEST_URI} !^/(blog/abc|blog/xxx|blog/events/go-to-town) [NC]
    The rewrite rules remain the same.
     
  3. Then you can add your custom rewrite rules above the </IfModule>.
Warning! Each line of code in the .htaccess file must have its own new line in the file. Do not allow a long line to break and continue on a new line or your code will have unexpected behavior. For example:
 
RewriteRule ^events/go-to-town$ "http://secure.hostgator.com/~affiliat/cgi-bin/affiliates/clickthru.cgi?id=whatever" [R=301,L]

This should all be one line.