htaccess url rewrite with question mark

2020-03-31 05:20发布

I'm having a problem in .htaccess, specifically on redirection.

I wanted to rewrite this url:

http://www.domain.com/?mod=country

to

http://www.domain.com/country

I already tried this .htaccess code:

RewriteEngine on
RewriteRule ^\?mod=(.*)$ $1

I tried working on it without the question mark character and it works fine. I have escaped the question mark because it's part of the special characters, but still, I couldn't get it work.

Can you tell what I'm missing?

1条回答
手持菜刀,她持情操
2楼-- · 2020-03-31 06:23

Directly translating your requirements to rewrite rules:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^mod=(.*)$
RewriteRule ^$ %1

But you probably want something more flexible like:

RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)mod=([^&]+)
RewriteRule /?(.*) %1/$1 [QSA]

These rules apply to the root of your domain.

If you want to do an external redirect add the R flag to the rewrite rule.

查看更多
登录 后发表回答