I am trying to convert a website to WordPress. The URLs in use are currently in the format: http://www.example.com/database/44/category/short-item-description.html

To display items that are stored in the database, I have created a page template DatabaseFetch.php and have added a page to WordPress to use this template at address http://www.example.com/database/ (this can also be access by http://www.example.com/index.php?p=39)

The template DatabaseFetch.php will have the PHP code to get the ID number from the URL and then use this ID number to get the relevant item details from the database.

I am trying to alter the .htaccess file found in the WordPress root folder as I want all URLs in the format of http://www.example.com/database/44/category/short-item-description.html to be processed by http://www.example.com/database/ but I want the browser to display the URL in the format of http://www.example.com/database/44/category/short-item-description.html (ie the URL not to change)

I have tried many combinations over the last couple days but I cannot get it working. Using the line:

RewriteRule ^/?database/([0-9]+)/?([a-zA-Z0-9_-]*)/?([a-zA-Z0-9_-]*).html$ /database/ [NC,L]

brings up WordPress 404 page.

Using the line:

RewriteRule ^/?database/([0-9]+)/?([a-zA-Z0-9_-]*)/?([a-zA-Z0-9_-]*).html$ /?p=39 [NC,L]

brings up correct page, but the URL in the browser changes to http://www.example.com/database/ Does anyone know how I need to alter the .htaccess file below to get the desired results I seek?

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# RewriteRule ^/?database/([0-9]+)/?([a-zA-Z0-9_-]*)/?([a-zA-Z0-9_-]*).html$  /database/ [NC,L]
RewriteRule ^/?database/([0-9]+)/?([a-zA-Z0-9_-]*)/?([a-zA-Z0-9_-]*).html$  /?p=39 [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


# END WordPress

I have also tried both lines with flags and no flags and combination of flags but nothing gets the desired result. On a non-Wordpress installation, I have the desired outcome defined by one line in .htaccess

RewriteRule ^/?database/([0-9]+)/?([a-zA-Z0-9_-]*)/?([a-zA-Z0-9_-]*).html$ database.php?id=$1 [NC,L]

The problem with WordPress is that the database page I need is not the physical DatabaseFetch.php file. therefore how do I go about the rewrite? Any help will be very much appreciated.


Don't use an .htaccess rule, use WordPress internal rewrite system.

First, add a query var for your ID:

function wpd_query_vars( $query_vars ){
    $query_vars[] = 'my_id';
    return $query_vars;
add_filter('query_vars', 'wpd_query_vars');

Then add an internal rule to catch incoming requests, load your database page, and set the ID query var:

function wpd_database_rewrites(){
add_action( 'init', 'wpd_database_rewrites' );

Then you can access the value of the ID via get_qury_var( 'my_id' );

Add a couple more query vars and set those via $matches[2] and $matches[3] if you also want category and title.

  • So close! This is exactly what I want except for one minor issue. The URL has now got a trailing forward slash. How can that be removed? http://www.example.com/database/44/category/short-item-description.html now becomes http://www.example.com/database/44/category/short-item-description.html/ – AlexanderR May 16 '14 at 19:18
  • A quick fix is to change the post permalink structure under Settings > Permalinks and remove the trailing slash there, which will allow no trailing slash for all permalinks. Otherwise I think you'd have to remove the canonical redirect function that WordPress uses to redirect to the version with slash, which may have other consequences. – Milo May 16 '14 at 19:34
  • Allelujah! Yes changing Permalinks to Custom Structure of http://www.example.com//%postname% has totally answered my question. You are a legend. I have been trying to get this answered for three-four days. Thank you! – AlexanderR May 16 '14 at 19:58

Your converting a website to Wordpress but you want to get data from an external database?

Getting it working in the way you are describing could be quite hacky. Why not add it to the Wordpress as a custom post type. Import all the data from the old website into the new Wordpress site and then just do 301 redirects from the old urls to the new ones?

That would take care of the URL rewriting issues and you would also have the added bonus of being able to edit the content from the Wordpress admin panel if you did this too.

Your Answer

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

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