I'm converting an existing website to a WordPress theme. One of the pages is a gallery that uses JS history.pushState to create unique URLs for loading certain images.

E.g. page URL (non WP website): http://www.good-deeds-day.org/gallery/

URL for a certain image: http://www.good-deeds-day.org/gallery/img-35

I would like to preserve this functionality in WordPress. However, when loading the URL for a certain image, WordPress considers it as a different page and displays a 404 error.

Is there a way to configure WordPress or .htaccess so that when such image URLs are loaded it will keep the URL and load the gallery page, thus displaying the relevant image in the gallery page?

2 Answers 2


I ended up using add_rewrite_rule:

function bones_rewrite_rules() {
    $gallery_page_id = 395; 
    add_rewrite_rule('^gallery/img-.*', 'index.php?page_id=' . $gallery_page_id, 'top');
add_action('init', 'bones_rewrite_rules');

But from your description it's not an URL to a gallery, but individual image of one. I would guess it should consistently show individual image for such URL, regardless if it's served by WP back-end or manipulated client-side.

The easiest way to accomplish such in WP I can think of would be rewrite endpoint. It can rather easily latch onto existing permalink structure and it's easy to work with and modify output.

Unfortunately your format /img-35 isn't endpoint-friendly, but if changing it is an option /img/35 would work with endpoint perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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