regex to redirect non www to www and subdomain to file
-
25-05-2021 - |
質問
I need to do the following .. have come across various examples but i need to combine three conditions
redirect 1) redirect non www / non subdomain requests. eg :
http://xyzsite.com to http://www.xyzsite.com
2) redirect if subdomain is mentioned . eg :
http://user1.xyzsite.com to http://www.xyzsite.com/profile?user1
3) redirect to mobile version. eg :
http://m.xyzsite.com to http://www.xyzsite.com/m
Tech details : I m on IIS ver 6 & using helicontech isapi_rewrite module
解決
1.
- Match:
^xyzsite.com$
- Redirect:
www.xyzsite.com
2.
- Match:
^(?!www.)(.*).xyzsite.com$
- Redirect:
www.xyzsite.com/profile?$1
3.
- Match:
^m.(.*)$
- Redirect:
www.$1/m
他のヒント
Here are the ISAPI_Rewrite v3 rules (hope this is the version you use):
RewriteBase /
RewriteCond %{HTTP_HOST} ^xyzsite\.com$
RewriteRule .? http://www.xyzsite.com [NC,R=301,L]
RewriteCond %{HTTP_HOST} ^m\.xyzsite\.com$
RewriteRule .? http://www.xyzsite.com/m [NC,R=301,L]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.xyzsite\.com$
RewriteRule .? http://www.xyzsite.com/profile?%1 [NC,R=301,L]
I spent some time on this to hopefuly get you in the right direction. I come up with easiest solution unless you specified a constraint expliciterly. This means that I hardcode xyzsite.com in the regex. This actualy higlight the essense more of the solution
1) redirect non www / non subdomain requests. eg : http://xyzsite.com to http://www.xyzsite.com
pattern:
http://(.*?.com)
replacement:
http://www.$1
2) redirect if subdomain is mentioned . eg : http://user1.xyzsite.com to http://www.xyzsite.com/profile?user1
pattern:
(http://)(.*?)\.(.*)
replacement:
$1www.$3/profile?$2
3) redirect to mobile version. eg : http://m.xyzsite.com to http://www.xyzsite.com/m
pattern:
http://m\.(.*)
replacement:
http://www.$1/m
Hope this helps, Buckley