如何将所有流量重定向到htaccess中的某些文件夹?

时间:2015-07-28 06:17:33

标签: 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 /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

1 个答案:

答案 0 :(得分:1)

您最好使用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

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