Hi all, I am sorry if I have posted this in the incorrect place but I couldn't see an Apache section.

I am working on some ReWrites and ReDirects in my htaccess file, my reason for doing this is because I am developing a new design and layout for an existing website therefore I need to redirect to keep hold of the google rankings etc.

So the old (existing) URL looks like this:

/news/internet-shopper-numbers-continue-to-grow-$21378232.htm

and the new URL will look like this:

/industry-news/small-business-website-design/internet-shopper-numbers-continue-to-grow/800715788

in the old URL "internet-shopper-numbers-continue-to-grow" is dynamically generated

where as in the new URL "small-business-website-design", "internet-shopper-numbers-continue-to-grow", and "800715788" are all dynamically generated.

So my issue is how do I get from 1 URL to another if only 1 of the dynamically generated variables is available. I was thinking perhaps it would be a good idea to do a PHP redirect instead where I can get the data from the URL process through database to get all data needed to generate the new URL, but im not sure if this is good practice, this would also mean having to match the old url file directory which is not the current case.

I'm guessing I start with something like this:

Options +FollowSymlinks
RewriteEngine On
RewriteCond /news/%{QUERY_STRING}(.)(\\d+)(.)(htm)$
RewriteRule ^industry-news/(.+)/%1/(\d{9})

I know this is very basic and kinda wrong but I'm working on this for the first time and im stumped.

Hope someone can help,
Cheers guys.

Dani AI

Generated

Short version: there are two practical, reliable ways to preserve SEO when changing URL structure — a server-side mapping (best when you can edit the vhost/apache config) or a small redirect lookup script (works on shared hosting). DNS isn’t involved here (that was the confusion in ’s reply). ’s manual approach works but is tedious; the methods below let you automate and scale the redirects instead of hand-writing 600+ entries.

If you control the Apache vhost, use RewriteMap (fast, no per-request DB hit). Define a flat mapping file and let Apache substitute the target on the fly:

# in server/vhost config (NOT .htaccess)
RewriteEngine On
RewriteMap old2new txt:/etc/apache2/redirects.map
RewriteCond %{REQUEST_URI} ^/news/[A-Za-z0-9-]+-([0-9]+)\.htm$ [NC]
RewriteRule ^ ${old2new:%1} [R=301,L]

Mapping file format (one key/value per line):

# /etc/apache2/redirects.map
123456789 /industry-news/some-category/some-slug/123456789
234567890 /industry-news/another-category/another-slug/234567890

Generate that map from your database (SQL export + a small script) — much faster and safer than typing entries by hand.

If you’re on shared hosting (no vhost access), redirect the old pattern to a PHP lookup that issues the 301:

# in .htaccess
RedirectMatch 301 ^/news/[A-Za-z0-9-]+-([0-9]+)\.htm$ /redirect.php?id=$1

In redirect.php do a single prepared DB lookup for the numeric id, cache the result (APCu/file), then send a 301 Location header to the new URL and exit.

Final notes and checks: always use 301 for permanent moves, test with curl -I to verify the exact response and that there are no redirect chains, add new URLs to your sitemap, and monitor server logs for missed IDs. If performance matters, prefer the RewriteMap approach or cache DB lookups to avoid per-request overhead.

Recommended Answers

All 2 Replies

The problem i see with that is: how are all the DNS servers going to know an address that keeps changing? it's like trying to find a homeless person(they're always moving).perhaps you could just use the variable 'x' for the # in the real address, then have the alternate address displayed everywhere else within the HTML code?

-edit, uh nevermind , i think read the post wrong

I have overcome this issue by individually creating it, spending hours and hours writing it out - not the best way but I needed to get it done. I redesigned a website and changed the url structure, the website has 600+ urls in this format and I was trying to avoid having to write out tonnes of unique redirects by hand. I could not find a solution so I just wrote it all out.

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.