Apache重写规则-路径样式

Apache重写规则-路径样式,apache,mod-rewrite,Apache,Mod Rewrite,我是从IIS世界来到Apache的,希望能在重写规则方面得到一些帮助 我想要这个相对路径: /index.php?go=order&id=12345 改写为: /go/order/id/12345 此外,如果有更多参数,则应将其转换为路径格式: /index.php?go=order&id=12345&action=process 变成 /go/order/id/12345/action/process 请问我如何做到这一点?感谢您的任何输入。尝试将其放入vhos

我是从IIS世界来到Apache的,希望能在重写规则方面得到一些帮助

我想要这个相对路径:

/index.php?go=order&id=12345
改写为:

/go/order/id/12345
此外,如果有更多参数,则应将其转换为路径格式:

/index.php?go=order&id=12345&action=process
变成

/go/order/id/12345/action/process

请问我如何做到这一点?感谢您的任何输入。

尝试将其放入vhost配置中:

RewriteEngine On

# Start converting query parameters to path
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?[^\ ]+
RewriteCond %{QUERY_STRING} ^([^=]+)=([^&]+)&?(.*)$
RewriteRule ^(.*)$ $1/%1/%2?%3 [L]

# done converting, remove index.php and redirect browser
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?[^\ ]+
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/index.php/(.*)$ /$1 [R=301,L]

# internally rewrite paths to query strings
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^/([^/]+)/([^/]+)/?(.*) /$3?$1=$2 [L,QSA]

# No more path, rewrite to index.php
RewriteRule ^/$ /index.php [L]
如果您键入像
http://yourdomain.com/index.php?a=b&1=2&z=x
在浏览器中,浏览器将被重定向到
http://yourdomain.com/a/b/1/2/z/x
。当请求外观整洁的URL时,第二组规则会在内部将其重写回
/index.php?a=b&1=2&z=x
。如果要将这些规则放入htaccess文件(在文档根目录中),则需要删除
重写规则
中的所有前导斜杠。因此,在最后3条规则中,
^/
需要是
^


请注意,如果您只需转到
http://yourdomain.com/index.php
,如果没有查询字符串,则不会重写任何内容。

太好了!现在我们如何检查给定的路径是否不是现有的目录或文件?静态资源没有加载这个。谢谢Jon!你今天教了我一些新东西!:)