Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Wordpress .htaccess rewrite for custom template WordPress

I am trying to convert a website to WordPress. The URLs in use are currently in the format: http://www.domain.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.domain.com/database/ (this can also be access by http://www.domain.com/?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.domain.com/database/44/category/short-item-description.html to be processed by http://www.domain.com/database/ but I want the browser to display the URL in the format of http://www.domain.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.domain.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]
</IfModule>

# END WordPress


I have also tried both lines with flags and no flags and combination of flags but nothing gets the desired result.

Answers (1)

2014-05-13

John Cotton answers:

Have you set and saved permalinks on the settings menu on the dashboard?

/dashboard/ would need a custom permalink of /%category%/%postname%/


alexristo comments:

I don't believe that is correct. My Permalink Settings are set to "Post name" e.g. http://domain.com/sample-post/

The Post name is "database". Category is not defined in WordPress and is not really important. The only thing that is important is the ID number.

The website will produce the same output for the following URLs (ie info about item 44)
http://www.domain.com/database/44/category/short-item-description.html
http://www.domain.com/database/44/blah/short-item-description.html
http://www.domain.com/database/44/camping/short-item-description.html



John Cotton comments:

Sure, /%postname%/ on it's own will do.

Are you wanting to rewrite http://www.domain.com/database/44/camping/short-item-description.html to

http://www.domain.com/database/camping/short-item-description

(ie each old url goes to a new unique url)

Or are you wanting everything to go to your /database page?


alexristo comments:

I want the URL to stay the same but everything to be processed by http://www.domain.com/database/ (which infact is http://www.domain.com/?p=39 or http://www.domain.com/index.php?p=39)

The custom template that http://www.domain.com/database/ uses will then use PHP to get the ID from the URL.


John Cotton comments:

<blockquote>The custom template that http://www.domain.com/database/ uses will then use PHP to get the ID from the URL.</blockquote>

In which case, you should do it all in WordPress.


function add_rewrite_rules($rules) {
$newrules = array();

$newrules['^database/(.+)/(.+)/(.+).html$'] = 'index.php?p=39&db_id=$matches[1]&db_term=$matches[2]&db_desc=$matches[3]';

return array_merge( $newrules, (array) $rules );
}
add_filter('rewrite_rules_array','add_rewrite_rules');

function add_query_vars($vars) {
array_push( $vars, 'db_id', 'db_term', db_desc' );
return $vars;
}
add_filter('query_vars','add_query_vars');


In the database page template, you can use get_query_var to access the value of each query string element.

Depending on your server config, you may need to add a line to the htaccess file to ensure that the .html extension actually gets handled by WordPress rather than just 404 by Apache or whatever you're using.


alexristo comments:

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

but the issue with WordPress is that there is no physical file to rewrite to as "/database/" is really "index.php?p=39"

If I try
RewriteRule ^/?database/([0-9]+)/?([a-zA-Z0-9_-]*)/?([a-zA-Z0-9_-]*).html$ /index.php?p=39

I get the correct page but the URL in the browser changes (which is not what I want).


alexristo comments:

I have just entered your PHP code to my functions.php via the WordPress editor but when I click on "Update File" it says error 403 Forbidden - Access to this resource on the server is denied!

Do you have any idea why?


John Cotton comments:

<blockquote>RewriteRule ^/?database/([0-9]+)/?([a-zA-Z0-9_-]*)/?([a-zA-Z0-9_-]*).html$ /index.php?p=39

I get the correct page but the URL in the browser changes (which is not what I want).</blockquote>

You don't need rule that if you use the code I gave you as the code handles the rewrite in PHP.


John Cotton comments:

<blockquote>I have just entered your PHP code to my functions.php via the WordPress editor but when I click on "Update File" it says error 403 Forbidden - Access to this resource on the server is denied!</blockquote>
...don't know, but really you should manage your edits via FTP. The 403 is not to do with the code change - promise!

PS Once you have saved the code, you need to save permalinks again to activate it.


alexristo comments:

I tried the code on another WordPress site (different server, different theme) and it broke functions.php and therefore broke the site. I am now trying to update manually but something funny is going on with the server as none of my usual credentials are working. I will reset the server now. Anyway their is a small error with your code

array_push( $vars, 'db_id', 'db_term', db_desc' );

should be

array_push( $vars, 'db_id', 'db_term', 'db_desc' );


But this change still didnt help me.


alexristo comments:

I have altered the functions.php via FTP and tested it. Unfortunately it doesn't work as I want it to work. It goes to the correct page but the URL changes to http://domain.com/database/


John Cotton comments:

Do you actually need that page? Is there content on there that you want to be able to edit, or are you just using it to create a url?

If the later, then:

a) Delete the page
b) Add the following code


function template_redirect() {
if( get_query_var('db_id') ) {
// Do whatever output you want. You could include a template or other file here for neatness

exit();
}
}
add_action( 'template_redirect', 'template_redirect' );


John Cotton comments:

Sorry missed off

c) remove p=39 from the rewrite (ie 'index.php?db_id=$matches[1]&db_term=$matches[2]&db_desc=$matches[3]';)


And a d) should probably be "re-save" permalinks.


alexristo comments:

<blockquote>Do you actually need that page? Is there content on there that you want to be able to edit, or are you just using it to create a url?</blockquote>

I am not sure by what you mean do I need the page?

The database page (custom template) will have PHP code that will use the ID from the URL to fetch the code via a select SQL statement.

I had a go at trying your suggested changes but it still doesn't work as desired. The URL stays the same but a completely blank page is returned.


alexristo comments:

Your code says

<blockquote>// Do whatever output you want. You could include a template or other file here for neatness</blockquote>

I am guessing I am getting no output as nothing is currently specified.
Can you please give me example code of what to put there? I obviously want the page to look like all the others but have the PHP MYSQL fetch data in the content window.

I am off to bed now. I will continue this tomorrow.


John Cotton comments:

<blockquote>Can you please give me example code of what to put there?</blockquote>
Er....you've not said what you code is doing?

Just execute whatever PHP code you were planning on executing in the page. You could do this if you wanted page output:


$post = get_post( 22 );
echo apply_filters( 'the_content', $post->post_content );


alexristo comments:

Ok John we are making some progress. I have update the code and now I get the content of the database page, without the theme styling. The URL stays the same (which is what I want!) What I see is uploaded to this question.

Now how do I get the theme shown on this page?