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

1
  • @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 Commented Jul 13, 2015 at 1:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much @Mario! I will try and use the above now and report back. Much appreciated.
That worked. Thank you so much! You have done in 2 minutes what would have taken me weeks. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.