SOLVED!!!! WordPress Rewrite Rule

by 9 replies
12
I need to rewrite the following URL

Example.com/hot-presidents.php?celeb=obama&val=big-fan

into

example.com/obama-hot-presidents/

I am trying to do this in WordPress with the following code.

#WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^([A-Za-z0-9]+)-hot-presidents/$ hot-presidents.php?celeb=$1&val=big-fan [L]
</IfModule>

What am I doing wrong?
#programming #rewrite #rule #wordpress
  • looks like you are missing a forward slash from before the hot-presidents.php.

    Try this:

    Code:
    RewriteRule ^([A-Za-z0-9]+)-hot-presidents/$ /hot-presidents.php?celeb=$1&val=big-fan [L]
    • [1] reply
    • Oh, and try putting it above the following line:

      RewriteRule ^index\.php$ - [L]

      I think this rule is interfering with it.
      • [1] reply
  • You shouldn't be touching the .htaccess file AT ALL.

    Login to Wordpress, click on Settings -> Permalinks.

    Set it to use a custom permalink structure that looks like:

    /%category%/%postname%

    Click save.

    Fixed.
    • [ 1 ] Thanks
    • [2] replies
    • Don't know why you'd say that - there are a lot of reasons that more advanced users would have to modify the .htaccess file.

      The OP is a case in point. Your solution wouldn't work, WP would just throw a 404.

      The permalink structure you suggest shouldn't be used, especially if you expect to use more than a few pages (not posts) on your site. The number of entries in the rewrite table grows exponentially when a non-numeric first section is used.

      While it may not LOOK as good, a better structure is
      Code:
      /%post_id%/%postname%/
      A permalink structure like that cuts the size of the lookup table substantially, and helps speed up your WP site.
      • [ 1 ] Thanks
      • [1] reply
    • Sometimes you need more than what WordPress offers. It's only for more sophisticated sites, not for regular users.
  • Why not touching .htaccess? It can be very useful to modify .htaccess file such as cloaking affiliate/referral link.

Next Topics on Trending Feed