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

.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?

图片转代码服务由CSDN问答提供 功能建议

我让用户从不同的来源(论坛,博客......)访问我的网站,链接不存在 没有了,所以出现404错误。 根据他们在我的网站上访问的链接,我知道在哪里发送它们。 所以链接是这样的:

  www.test.com/shop/customers/individuals/343a/?shopc=variable
www.test.com/shop/customers/ 公司/ 445A /?shopc =可变
www.test.com/shop/b2b/discount/43b/?shopc =可变
 ... 
   
 
 

和 等等

如您所见,所有链接都有www.test.com/shop/和变量shopc。 我想要做的是重定向到正确的位置。 让我举两个用户访问我的网站和我想做的例子。 所以用户使用以下网址进入我的网站:

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

现在显示404错误,因为该文件和目录 < 预> <代码> /hsphere/local/home/danuser/test.com/shop/low/index.php \ N / hsphere /本地的/ home / danuser / test.com /店铺/公司/ B2B / index.php的\ n

我的网站上不再存在。 但基于shopc变量,我知道在哪里重定向它们。

所以问题是:我可以创建一个.htaccess文件来重定向链接,如

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

n

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

所以我可以抓住shopc变量并将它们重定向到 正确的位置?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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
    )
    
    打赏 评论
  • dongxing2015 2015-03-14 12:35

    It took me some time to find the answer to your question. The problem is that the query-string is not matched with the mod_rewrite regex.

    The code that should solve your problem is the following:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} shopc=(\w+)
    RewriteRule ^(\w+/)*$ index.php [L]
    

    It uses an RewriteCond to check that your variable shopc is set.

    That's why my first rule did not work and why escaping the ? character in an RewriteRule is not the solution for your question.

    #RewriteRule ^(/\w)+/?shopc=(\w+)$ index.php?shopc=$2 [NC,L]
    

    Anyways I do suggest you not to use .htaccess if you have access to the httpd config, because .htaccess is slow in comparison to a propper server configuration. Because with .htaccess the webserver must look in each directory for a .htaccess file.

    打赏 评论

相关推荐 更多相似问题