I am trying to redirect a directory and all pages e.g. /shops/merchant1.htm to a generic file generic.htm using htaccess, does any have any ideas how to do this?

Also the generic.htm contains php.

I currently have hundreds of shops each with its own htm page, but i really want one generic.htm page that will cover all the shops, but driven by php and a mysql database. This would save dramatically on admin work generating a new page for each new merchant when it could be driven by one page reading the url address to modify the page to suit the new merchant.

Cheers,

Andrew

Dani AI

Generated

For : the simplest, reliable approach is to let one PHP script serve all merchant URLs and have Apache rewrite incoming requests for /shops/merchant1.htm to that script while keeping the friendly URL in the browser. is right that cPanel can create redirects, but those are usually external redirects (they change the URL). correctly flagged the PHP/extension issue — servers do not parse .htm as PHP by default unless configured.

A common .htaccess solution (placed in your site root) uses mod_rewrite to internally map requests to the backend script and pass the merchant identifier as a query parameter:

RewriteEngine On
RewriteRule ^shops/([^/]+)\.htm$ /generic.php?shop=$1 [L,QSA]

This keeps /shops/merchant1.htm visible while serving content from generic.php. If the rule lives inside the shops/ folder, drop the shops/ prefix from the pattern. See the Apache mod_rewrite documentation for pattern behavior and flags: mod_rewrite documentation.

If you prefer to keep .htm as the actual script file, you can tell Apache to treat .htm as PHP (host permitting) via an AddHandler/AddType directive — check your host’s policy first. See Apache’s handler docs: AddHandler (mod_mime).

Practical notes: enable mod_rewrite and ensure AllowOverride permits .htaccess usage, test rewrites with curl or browser dev tools, and log rewrite activity if you need to debug. Sanitize the shop input in generic.php and use parameterized queries (PDO prepared statements) to avoid SQL injection: PHP PDO prepared statements.

Recommended Answers

All 2 Replies

I have done this but using cpanel on my web hosts(going to the redirect link and/or page on the cpanel of the web host), i think it could be done, given time

Hi there

Lots of results from Google for general redirects.

Inside your .htaccess file -
Redirect 301 /oldpage.html

or using meta tags in your page header:
<meta http-equiv="refresh" content="2;url=http://target.com">

Just an aside - you mention that your generic.htm contains php? You do realise that it must then be called generic.php in order for the php interpreter to pick it up?

Hope this helps. 8-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.