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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器