Stumped on something that ought to be simple and wondering if we have an mod-rewrite gurus that have some ideas...

I have urls in the form of:

http://olddomain.com/report?view=report&reportId=63

the only significant thing to me here is that report id, which is now something like:

I already have the base old domains redirecting to the base of the new domain and can pick up the pattern match on the report id, but can't seem to get the rewriterule to the new domain.

thoughts much appreciated!!

cheers,

Jim

Dani AI

Generated

— the core issue is that Apache will append the original query string unless you explicitly discard it. The practical options are: 1) a small per-ID redirect in the old host, 2) a scalable server-side mapping using RewriteMap, or 3) let the new site perform the final 301 after looking up the ID. ’s idea of embedding the ID into the pretty URL is valid, but since the new site is already SEO-complete you need a lookup/translate step on the old host (or a quick relay on the new host).

Example approaches (old host):

Per-ID (simple, in .htaccess):

RewriteEngine On
RewriteCond %{QUERY_STRING} reportId=63
RewriteRule ^report$ https://newdomain/a-specific-seo-friendly-named-page? [R=302,L]

Scalable (server config; uses a text map file):

# in server/vhost config
RewriteMap reportmap txt:/etc/apache2/reportmap.txt

RewriteEngine On
RewriteCond %{QUERY_STRING} reportId=([0-9]+)
RewriteCond ${reportmap:%1} ^(.+)$
RewriteRule ^report$ https://newdomain/${reportmap:%1}? [R=302,L]

On Apache 2.4 you can also use the QSD flag instead of appending ? to strip the query string: [R=302,L,QSD].

Testing and troubleshooting: test with temporary R=302 first. Inspect headers with curl -I 'http://olddomain/report?view=report&reportId=63' to confirm the Location header. Ensure mod_rewrite is enabled and that RewriteMap is declared in server/vhost (it will not work inside .htaccess). Increase rewrite logging with LogLevel rewrite:trace3 to see why a rule did or did not match.

SEO notes: switch to R=301 only after testing. If maintaining a large ID→slug map is hard on the old host, have the old host redirect to a tiny lookup script on the new host that issues the final 301 after resolving the slug. For mod_rewrite reference see mod_rewrite.

Recommended Answers

All 2 Replies

The simplest way I know of doing this is by making the SEO'ed url into: domain.com/seo-friendly-name-63

The actual dynamic page could then use regex to fetch the number from the end of the page title string and know what report ID # to fetch.

thanks for the reply .. the key is that new site is already SEO complete (and working well!). Now we need to changeover from the old style urls and make the numbers go away completely. The problem seems to be all the new urls get the query string appended and I just need that NOT to happen. thanks!!

Jim

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.