简体   繁体   中英

.htaccess Query String Rewrite

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM