开发者

problem in htaccess regex phrase

I want to convert this url:

module-apple-get.html?term=st

to

file.php?module=apple&func=get&term=st

I 've wrote this code in .htaccess file:

RewriteRule ^module-apple-get\.html?term=([^-]+)$ file.ph开发者_运维问答p?module=apple&func=get&term=$1 [L,NC,NS]

but it doesn't work.is it wrong?


The RewriteRule directive doe not work with query string directly therefore your rule will never work.

Here are few approaches.

1) This will do the rewrite if /module-apple-get.html is requested and will append existing query string to the new URL. This means, that if you request /module-apple-get.html?term=st&some=another_param it will be rewritten to file.php?module=apple&func=get&term=st&some=another_param. This is a safer and recommended approach.

RewriteRule ^module-apple-get\.html$ file.php?module=apple&func=get [QSA,L]

2) Another approach is to ONLY rewrite if requested URL has term=st PRESENT:

RewriteCond %{QUERY_STRING} (^|&)term=st
RewriteRule ^module-apple-get\.html$ file.php?module=apple&func=get&term=st [L]

This will rewrite if you request /module-apple-get.html?term=st but will do NOTHING if you request /module-apple-get.html?some=another_param.

3) Yet another approach is to ONLY rewrite if WHOLE requested URL matches:

RewriteCond %{QUERY_STRING} ^term=st$
RewriteRule ^module-apple-get\.html$ file.php?module=apple&func=get&term=st [L]

This will rewrite if you request /module-apple-get.html?term=st but will do NOTHING if you request /module-apple-get.html?term=st&some=another_param.

P.S.

  1. You can add any other flags if required ([NC], [NS] etc).
  2. You may need to add leading slash / before file.php (depends on your setup, where this .htaccess is located etc)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜