Modifying wordpress htaccess to forward certain urls to subdirectory

For whatever reason or another I haven’t been able to ascertain, my company has decided to go with wordpress for one of their websites. They asked me to build an affiliate application on the same domain, which I did. Everything works great with the exception of this dilemma:

wordpress is installed in the root directory. All pages, videos, sales, etc are made from within wordpress pages.

Read More

The affiliate application is in a subdirectory /aff/ and affiliates’ pages are found at mydomain.com/aff/index.php?aff=affiliateusername

Affiliates (and their leads) should be able to load their pages simply by typing in www.mydomain.com/affiliateusername but I am struggling to understand how to translate wordpress htaccess rules to do this.

Obviously the best order in which to have this work is for wordpress to first determine if there are any blogs/posts/pages that match the url term FIRST, and if none is found, then to redirect all else to www.mydomain.com/aff/index.php?aff=whatever

Here’s what I was finally able to come up with that works for the index page and for the affiliate pages, but does not correctly load any wordpress pages other than index.

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

RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(images|wp-admin|wp-content|wp-includes|go|compliance.html)($|/) - [L]
#RewriteRule ^([^/].*)$ /aff/index.php?aff=$1 [R,L]
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

You can tell I’ve attempted to exclude certain directories from the rewrite but have not been successful. I’ve read other advice via Googling, to put the redirect rules ahead of the wordpress block, but there are few issues. When I put this line ahead of the # BEGIN WordPress line, I get an endless redirect loop which keeps going to /aff/index.php?aff=aff/index.php?… etc (this is the same line I use for the same affiliate application on a different, wordpress-free, domain)

#RewriteRule ^([^/].*)$ http://www.mydomain.com/aff/index.php?aff=$1 [L]

I feel like I’m missing something terribly obvious. Should I just be setting up wordpress to redirect all 404’s to /aff/index.php?aff=originalrequest? How would I go about doing that?

Thanks in advance.

Related posts

Leave a Reply

1 comment

  1. You are using RewriteRules incorrectly in place of RewriteConds. Adding them in between the WordPress rules is surely breaking your blog as well. Change your .htaccess code to:

    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
    
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^([^/.]+)$ /aff/index.php?aff=$1 [R=301,QSA,L]
    
      RewriteRule ^index.php$ - [L]
    
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]
    </IfModule>
    

    If you want the URL to stay mydomain.com/affiliate remove the [R] and use only [QSA,L].


    I’ve updated the rules above to show how to exclude a path from affiliate redirection. The following

    RewriteCond %{REQUEST_URI} !^/blogb
    

    excludes all URLs pointing to /blog or its sub-directories /blog/sub/dirs from redirection.


    If there are root-level .php pages present (even if they are few) the exclusion can more easily be handled by changing the rule to

    RewriteRule ^([^/.]+)$ /aff/index.php?aff=$1 [R=301,QSA,L]
    

    assuming that a . and a / can never be present in an affiliate name.