使用相同代码管理多个HTML的最佳方法?

I am building my own photography website where dynamic content is the pictures.

Right now, I have seperate HTML files for each page, which is very inefficient when I make any code changes.

I tried javascript to handle the dynamic content, but back and forward navigation doesn't work. I am not sure if php is appropriate as it's server side scripting.

What would be an optimal way to update the dynamic content?

Your question is not clear. It seems that you are simply duplicating each dynamic page to create a static HTML file.

Of course you should stop ASAP this, and you should use a proper framework to manage your dynamic content. Since you have mentioned PHP I suggest you to take a look at Laravel

You could create a route like:

yoursite.com/image/id-image

That can be managed by Laravel by:

Route::get('image/{id}', function($id) {
   ...
});

What you need it's some sort of templating engine, if you have experience with javascript jade would be a good option for you.