如何编写.htaccess重写规则以将查询字符串转换为分段URI?

时间:2021-07-16 10:38:27

Is there a way to put a rewrite rule in htaccess that will convert query strings to segmented URIs? For eg.

有没有办法在htaccess中放置重写规则,将查询字符串转换为分段URI?例如。

http://domain.com/controller/method/?a=1&b=2&c=3

should get rewritten as

应该改写为

http://domain.com/controller/method/a/1/b/2/c/3

All there is to do is remove the ? and replace the &s and the =s with a /, but I'm not quite sure how all that figures into .htaccess...

所有要做的就是删除?并使用/替换&s和= s,但我不太清楚所有这些是如何形成的.htaccess ...

Not that it'd matter, but I'm using PHP and CodeIgniter on Apache.

这并不重要,但我在Apache上使用PHP和CodeIgniter。

3 个解决方案

#1


Why don't you put that logic inside your CodeIgniter controller instead of doing it in .htaccess ?

为什么不将这个逻辑放在CodeIgniter控制器中而不是在.htaccess中?

Check CodeIgniter's User Guide for handling Query Strings and URLs.

检查CodeIgniter的用户指南,以处理查询字符串和URL。

#2


If you want to do it in the .htaccess you need to have mod_rewrite.

如果你想在.htaccess中执行它,你需要有mod_rewrite。

The rule is not too difficult. I give you a minimal example, and you need to tune the regex up to fix your need.

规则并不太难。我给你一个最小的例子,你需要调整正则表达式来修复你的需要。

RewiteRule ^/([a-z]+)/([a-z]+)$ /method/?$1=$2

#3


This is what you could do:

这是你可以做的:

RewriteCond %{THE_REQUEST} ^GET\ /[^?]*\?
RewriteCond %{QUERY_STRING} ^([^&=]+)=?([^&]*)&([^&=]+)=?([^&]*)&?(.*)
RewriteRule ^ %{REQUEST_URI}/%1/%2/%3/%4?%5 [L]
RewriteCond %{THE_REQUEST} ^GET\ /[^?]*\?
RewriteCond %{QUERY_STRING} ^([^&=]+)=?([^&]*)&?(.*)
RewriteRule ^ %{REQUEST_URI}?/%1/%2?%3 [L]

#1


Why don't you put that logic inside your CodeIgniter controller instead of doing it in .htaccess ?

为什么不将这个逻辑放在CodeIgniter控制器中而不是在.htaccess中?

Check CodeIgniter's User Guide for handling Query Strings and URLs.

检查CodeIgniter的用户指南,以处理查询字符串和URL。

#2


If you want to do it in the .htaccess you need to have mod_rewrite.

如果你想在.htaccess中执行它,你需要有mod_rewrite。

The rule is not too difficult. I give you a minimal example, and you need to tune the regex up to fix your need.

规则并不太难。我给你一个最小的例子,你需要调整正则表达式来修复你的需要。

RewiteRule ^/([a-z]+)/([a-z]+)$ /method/?$1=$2

#3


This is what you could do:

这是你可以做的:

RewriteCond %{THE_REQUEST} ^GET\ /[^?]*\?
RewriteCond %{QUERY_STRING} ^([^&=]+)=?([^&]*)&([^&=]+)=?([^&]*)&?(.*)
RewriteRule ^ %{REQUEST_URI}/%1/%2/%3/%4?%5 [L]
RewriteCond %{THE_REQUEST} ^GET\ /[^?]*\?
RewriteCond %{QUERY_STRING} ^([^&=]+)=?([^&]*)&?(.*)
RewriteRule ^ %{REQUEST_URI}?/%1/%2?%3 [L]