0

I've got the following ReWrite rule:

add_rewrite_rule("details\/([a-zA-Z0-9_]+)\/.*", "detail/?id=$matches[1]", "top");

Which seems to be adding in the following to my .htaccess, which now looks like this in its entirety:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^details\/([a-zA-Z0-9_]+)\/.* /detail/?id= [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

This doesn't seem to work. I'm a bit unsure as to why it hasn't put $1 on the end of the rewrite rule. Anyway, I manually added that, and it seems to then be doing something. I can see that the title of the page has changed to that of the rewritten page. The page itself however is still returning a 404 and the standard "OOPS! THAT PAGE CAN’T BE FOUND." message appears.

What have I done wrong?

  • There are two rewrite systems in WP- internal and external. You have an external rule (it doesn't point to index.php), but you've used $matches[1], which is PHP, and only works for internal rules. What is detail in this case- is that a page post type? Is id a registered query var? – Milo Dec 4 '18 at 16:48
  • detail is a page created in wordpress with a shortcode that produces the page content via a plugin. The shortcode handles the id query variable – Alan Dec 4 '18 at 16:52
  • Rewrite rules can't be used to change pretty permalinks into a different type of pretty permalinks, nor are they redirects. Rewrite rules take pretty URLs and rewrite them into index.php?quaryvar=value&etc.... You can't use them on any other files. It's more like a mapping/router, and has little to do with Apache HTAccess rewrite rules, or Nginx rules – Tom J Nowell Dec 4 '18 at 17:28
  • So is what I'm trying to do unachieveable? I have what is essentially an unfriendly url in mysite.com/detail/?id=123 which as mentioned is generated via a shortcode in a plugin. I'm trying to rewrite that as something more friendly like mysite.com/details/some-description-goes-here. From what I can tell the Apache rewrite rule IS working - as it appers to be executing some of the shortcode, it's just wordpress isn't recognising it as a valid page and throwing a 404 – Alan Dec 5 '18 at 9:26
  • I'll assume the answer is no then. Is there no way of Wordpress not interpreting trailing slashes as new pages ? mysite.com/thispageexists/id/something-else-for-seo-purposes. No way to tell it to ignore what follows 'thispageexists' ? to prevent the 404 – Alan Dec 10 '18 at 16:09

Your Answer

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

Browse other questions tagged or ask your own question.