Mod_rewrite rewrite example.com/page.php?v1=abc&v2

2019-03-06 15:06发布

问题:

Using Apache's htaccess file, I am trying to rewrite the URL http://example.com/page.php?v1=abc&v2=def to http://example.com/abc/def.

So far I have:

Options +FollowSymLinks
RewriteEngine on
RewriteRule case-study/(.*)/(.*)/(.*)/(.*)/$ /case-study.php?$1=$2&$3=$4

But this seems to just serve the actual file page.php with the URL unchanged.

Does anyone know what the proper way to do this or what might be wrong with the above?

回答1:

You have it backwards. What you're rewriting from goes first, then what you're changing it to goes at the end. The following may not be exact, but should be close to what you need:

RewriteCond {%QUERY_STRING} ^v1=(.*)&

RewriteCond {%QUERY_STRING} v2=(.*)$

RewriteRule case-study.php /case-study/%1/%2/

Here's the RewriteRule documentation for more details: mod_rewrite

Note that the use of leading slashes on the rule and substitution will depend on the context you're using it in (see the linked documentation). In the above example, the %1 and %2 refer back to the capture groups in the first and second RewriteCond. Here's a page that describes that approach: mod_rewrite based on query string parameters