douwu7168 2015-03-14 11:59
浏览 382
已采纳

.htaccess将所有子目录重定向到新位置

I have users coming to my website from different sources (forums, blogs...) with links that does not exist no more, so a 404 error shows up. Base on the link they access on my website, I know where to send them. So the links are like this:

www.test.com/shop/customers/individuals/343a/?shopc=variable
www.test.com/shop/customers/company/445a/?shopc=variable
www.test.com/shop/b2b/discount/43b/?shopc=variable
...

and so on

As you can see all the links have www.test.com/shop/ and the variable shopc . What I want to do is to redirect to the correct location. Let me give you an example of 2 user accesing my website and what I want to do. So the users are entering my website with this urls:

www.test.com/shop/low/index.php?shopc=variable
www.test.com/shop/company/b2b/index.php?shopc=variable

Right now a 404 error is displayed because that files and dir

/hsphere/local/home/danuser/test.com/shop/low/index.php
/hsphere/local/home/danuser/test.com/shop/company/b2b/index.php

does not exist on my website no more. But base on the shopc variable I know where to redirect them.

So the question is: Can I create a .htaccess file to redirect links like

www.test.com/shop/company/b2b/user/index.php?shopc=variable

to

www.test.com/shop/index.php?shopc=variable

so I can grab the shopc variable and redirect them to the proper location?

  • 写回答

2条回答 默认 最新

  • dongmouhao7438 2015-03-14 12:44
    关注

    In the shop directory's .htaccess file:

    RewriteEngine On
    RewriteBase /shop
    
    RewriteRule ^.*$ index.php [L,QSA]
    

    The flag QSA automatically appends the query string on to the substitute request. That's how 'shopc' is passed along. See Apache mod_write online documentation for more.


    When I go to this URL:

    http://localhost/shop/company/b2b/user/index.php?shopc=variable

    ...I get this output from print_r($_GET) in my /shop/index.php file:

    Array
    (
        [shopc] => variable
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog