我想在多个条件下使URL友好。
我得到了这个:www.example.com/?lang=en&page=test&model=mymodel
我想要:www.example.com/en/test/mymodel
但是我也得到了这个(带有其他参数):
www.example.com/?lang=en&otherpage=othertest&othermodel=myothermodel
必须是:
www.example.com/en/othertest/myothermodel
如何为我的整个网站执行此操作?
如果要使用如下所示的友好 URL:
www.example.com/<language>/<value1>/<value2>
那么 Apache将无法区分您提到的第一个和第二个"非友好"URL:
www.example.com/?lang=en&page=test&model=mymodel
www.example.com/?lang=en&otherpage=othertest&othermodel=myothermodel
这是因为参数名称(第一个 URL 中的page
和model
,第二个 URL 中的otherpage
和othermodel
)不存在,并且无法从友好 URL 中猜到。
可能的解决方法取决于您有多少种不同的方案,即要处理多少个不同的参数。
例如,如果您只有几个场景,则可以在友好的URL模式中添加一个部分,告诉Apache要使用哪些参数名称,如下所示:
www.example.com/<language>/<parameter_set>/<value1>/<value2>
然后,告诉 Apache 使用第一个参数集,如果<parameter_set>
等于 1,第二个参数集,如果它等于 2,依此类推。
示例重写规则集可以是:
RewriteEngine On
RewriteRule ^([w]+)/1/([w]+)/([w]+)$ ./?lang=$1&page=$2&model=$3
RewriteRule ^([w]+)/2/([w]+)/([w]+)$ ./?lang=$1&otherpage=$2&othermodel=$3
请注意,1
和2
是完全任意的(它们可以是任何其他字符串)。
当然,官方文档可以提供帮助。