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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化