Trying to redirect visitors from www.domain.com/abcto www.domain.com/company/?code=abc,

After some research and some messing around, I am currently using the code below, which doesn't work. I flush the permalinks after each attempt.

function custom_rewrite_basic() {
   add_rewrite_tag('%code%', '([^&]+)');
   add_rewrite_rule('([A-Za-z0-9]+)$', 'company/?code=$1', 'top');
add_action('init', 'custom_rewrite_basic');

My .htaccess page updates, and is the following:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^([A-Za-z0-9]+)$ /company/?code=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

Can anyone identify the issue?

  • use the full path (starting with index.php), otherwise it goes through more rewrites, or try putting it last in priority
    – inarilo
    Jan 1, 2018 at 12:52
  • The final landing page "/company/?code=$1" is not based on the index page. hence the use of $1 instead of $match[1]. Ill try putting it last
    – Matt
    Jan 1, 2018 at 21:38
  • all wp links point to the index page in root, what we see is the pretty url
    – inarilo
    Jan 2, 2018 at 5:51
  • Is this still the case? See Otto's response:wordpress.stackexchange.com/questions/27182/…
    – Matt
    Jan 2, 2018 at 8:53
  • so you mean this page is not a WP page? the other question is about redirecting directly to a php file inside a plugin folder.
    – inarilo
    Jan 2, 2018 at 14:31

1 Answer 1


If company is the page slug try


If you want it in .htaccess to intercept it before it goes to index.php try


If company is a folder, there should be an index.php (or whatever the default file name is for your server) inside it that will process code.

  • Thank you. This works to an extent but now /cpanel doesn't work. Can this particular address be excluded?
    – Matt
    Jan 4, 2018 at 8:42
  • is cpanel the only address that will ever need to be excluded? otherwise it may be safer to change it from /abc to /code/abc or /code-abc, or anything that will uniquely identify code urls.
    – inarilo
    Jan 4, 2018 at 11:49
  • Moved some pages to code-abc layout, just cpanel at the moment. Thanks!
    – Matt
    Jan 5, 2018 at 22:03
  • If this answered your question, please mark as accepted :)
    – inarilo
    Jan 6, 2018 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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