I am wanting to add a custom rewrite rule to my Wordpress site (on my server).
The rule is to rewrite (not redirect) a specific URL (that doesn’t conflict with any wordpress URLs) to a particular post, passing a variable, eg: mysite.com/card/card-name/ would rewrite to index.php?page_id=25&card=card-name mysite.com/card/othercard/ would rewrite to index.php?page_id=25&card=othercard
I think page_id is the correct parameter to pass the page/post number.
Oooowwhh! You’re making my head spin! Is it 25, 215 or 216?
You’ve also changed your code to include a directory marker (trailing /) which is preventing your first RewriteRule from matching. Where did that come from?
# On the assumption that you're dealing with Apache 2.x
# and this is in the .htaccess in the DocumentRoot:
RewriteEngine On
RewriteRule ^card/(.*)$ index.php?page_id=216&card=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? index.php [L]
Note, please, that I’ve removed the ? making the EVERYTHING atom following card mandatory - NOTHING OR EVERYTHING IS ALREADY OPTIONAL!
Also, the trailing / in the “card” regex was removed as that was preventing a match.
Both redirects were using a / as the first character (directing to DocumentRoot). Because you ARE in DocumentRoot already AND the / can cause some upset with Apache (it confuses that with the physical path to root when there is some other problem and the only “fix” I’ve found is to restart Apache - best to avoid this potential problem), I’ve also eliminated the superfluous leading /'s.
NOW it should work.
WP is a fine CMS tool but you do have to use special care with additional mod_rewrite statements, primarily ORDERing properly, to be sure that you’re not causing problems.
That sounds like a Wordpress problem. The guys and gals in the Wordpress forums can probably help you out with that. I can’t, I’ve never used wordpress
Also, you may want to get rid of the <IfModule> and </IfModule>. Once you have established mod_rewrite is installed and works there is no need to ask apache on each request if it is. Only consumes resources for nothing
You also need to be aware that ORDER is critical (especially when WP is designed to capture EVERYTHING left over after the NOT directory and NOT file conditions. IMHO, move your NEW (card) mod_rewrite statement to the TOP of the order and do NOT forget ScallioXTX’s Last flag.
He also save you from my rants about the <IfModule> - that’s VERY abusive of a server - and RewriteBase (yes, I KNOW they are both what WP installed but they’re NOT needed AND they are not appropriate)!
Question: Is id=25 (or id=215) a fixed value? If, as it appears, it is, no problem, otherwise WHERE did the id value come from?
Thanks, I have actually come up with another way of doing it (before I saw your post David), instead of rewriting to index.php (which kept redirecting me), I created a file called index2.php, and index2.php includes index.php.
I believe (by doing some simple debugging) that the redirect was occurring because the “REQUEST_URI” was not a “pretty URL” so in index2.php before the include of index.php I added a line that forced $_SERVER[‘REQUEST_URI’] = “/card” - the permalink for the post that I wanted to rewrite to. This prevented the redirect from happening, because now the REQUEST_URI was “pretty” and not just index.php/index2.php.
Now I will check my htaccess and see if I can fix it up to match your corrections David.
Yes, the 216 is static. It is the wordpress page that I want this custom “SE friendly” URL to be rewritten to, passing this card variable - which the WP page will reference.
Thanks, made a step forward, now wordpress or apache is doing some funny redirects (url is changing in the browser). I suspect that the rewrite is working, but then it gets redirected. If I manually enter the “mysite.com/index.php?page_id=216&card=something” in the browser, the page also gets redirected.