Regex 如何重定向htaccess中除特定文件夹之外的所有流量?

Regex 如何重定向htaccess中除特定文件夹之外的所有流量?,regex,apache,.htaccess,redirect,Regex,Apache,.htaccess,Redirect,这件事我一直在胡思乱想。我想将所有流量重定向到我的新站点 为了便于讨论,新网站是mysite.org。但是,我想将某些页面、规则和文件夹重定向到新站点上-如果用户不访问这些文件夹或规则-它应该是对我站点的通用重定向吗 redirect 301 / http://example.org redirect 301 /index.php http://example.org redirect 301 /index.html http://example.org redirect 301 /contac

这件事我一直在胡思乱想。我想将所有流量重定向到我的新站点

为了便于讨论,新网站是mysite.org。但是,我想将某些页面、规则和文件夹重定向到新站点上-如果用户不访问这些文件夹或规则-它应该是对我站点的通用重定向吗

redirect 301 / http://example.org
redirect 301 /index.php http://example.org
redirect 301 /index.html http://example.org
redirect 301 /contact.php http://example.org/contact-us
redirect 301 /store-locator/list.php http://example.org/store-locator
redirect 301 /store-locator/ http://example.org/store-locator
redirect 301 /about.php http://example.org/about-us
redirect 301 /customer-care.php http://example.org/contact-us
redirect 301 /hq-location.php http://example.org/store-locator
redirect 301 /privacy.php http://example.org/privacy
redirect 301 /terms.php http://example.org/terms
redirect 301 /policy.php http://example.org/returns

您最好使用
RedirectMatch
而不是
Redirect
,因为
RedirectMatch
使用正则表达式并只匹配匹配的URI,而
Redirect
匹配以提供的模式开头的任何URI,因此您的第一条规则将始终执行,并生成所有后续规则无效

RedirectMatch 301 ^/$ http://example.org
RedirectMatch 301 ^/index\.(html|php)$ http://example.org
RedirectMatch 301 ^/contact\.php$ http://example.org/contact-us
RedirectMatch 301 ^/store-locator/list\.php$ http://example.org/store-locator
RedirectMatch 301 ^/store-locator/?$ http://example.org/store-locator
RedirectMatch 301 ^/about\.php$ http://example.org/about-us
RedirectMatch 301 ^/customer-care\.php$ http://example.org/contact-us
RedirectMatch 301 ^/hq-location\.php$ http://example.org/store-locator
RedirectMatch 301 ^/privacy\.php$ http://example.org/privacy
RedirectMatch 301 ^/terms\.php$ http://example.org/terms
RedirectMatch 301 ^/policy\.php$ http://example.org/returns

请确保在清除浏览器缓存后对其进行测试。

谢谢您的回答/store locator/将不会重定向,但/store locator将-尾部斜杠?您也应该在Chrome开发中测试,并禁用缓存。