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

你说的曾经没有我的故事 提交于 2019-12-04 06:00:47

问题


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



来源:https://stackoverflow.com/questions/14363132/mod-rewrite-rewrite-example-com-page-phpv1-abcv2-def-to-example-com-abc-def

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!