0

Have been going round in circles for a few hours, would really appreciate a little help.

I am trying to rewrite URLs that include a query string (preceded by company.php).

I found a few ways to get the rewrite itself to work, but it always shows a 404 :(

For example I need to rewrite (using example value):

http://test.domain.com/company.php?companynumber=05614768

to:

http://test.domain.com/company/05614768/

Here is a couple I have managed to get to do the rewrite but are 404ing:

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/company\.php$
RewriteCond %{QUERY_STRING} ^companynumber=([0-9]*)$
RewriteRule ^(.*)$ http://test.domain.com/company/%1/? [R,L]

and

RewriteEngine On
RewriteCond %{QUERY_STRING} ^companynumber=([0-9]*)$
RewriteRule ^company\.php$ http://test.domain.com/company/%1/? [R,L]

I am guessing I need to figure out how do an internal redirect as well as the external redirect? But I am really struggling...

Centos 6.6 / Apache 2.4.12

ColinMcDermott
  • 347
  • 2
  • 8
  • @mario - If I understand, yes I think so. I am trying to get it to do an internal redirect as well as the external redirect (which I think is working). Thanks – ColinMcDermott Jul 13 '15 at 01:50

1 Answers1

1

Now if that's your old-to-new redirect:

RewriteCond %{QUERY_STRING} ^companynumber=([0-9]*)$
RewriteRule ^company\.php$ http://test.domain.com/company/%1/? [R,L]

Then remap the new incoming URL via:

RewriteRule ^company/(\d+)/?$ company.php?companynumber=$1 [END]

Note that this:

  • Must be a rule thereafter.
  • Should carry the [END] flag to avoid another internal rewrite round.
  • Only allows numeric \d+ placeholders.

See also "Ping-Pong rewrites" on Reference: mod_rewrite, URL rewriting and “pretty links” explained. These rewrites should just be used as temporary measure. Establish the new URLs right in your HTML templates.

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291