1

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?

Dan
  • 57
  • 1
  • 10

2 Answers2

0

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.

Community
  • 1
  • 1
Jakob Alexander Eichler
  • 2,988
  • 3
  • 33
  • 49
0

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
)
Dan
  • 57
  • 1
  • 10
bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37
  • 1
    What error do you get? Is the .htaccess in the "shop" directory? I tested it again on my computer and it's working. We may have some different configurations. – bloodyKnuckles Mar 15 '15 at 23:35
  • I can send you my testing server info so you can test it. I have no idea how to make it work. htaccess is working (enabled) – Dan Mar 16 '15 at 23:54
  • 1
    First, what directory is the *.htaccess* file in? Starting at server root. Next question, what directory is the *index.php* file in? – bloodyKnuckles Mar 16 '15 at 23:57
  • The guy is abusing our expertise by posting the same responses on both our comments without taking the effort to get two valid solutions working. Both solutions are kind of valid because the question was asked so badly that it was not clearly specified what he liked to obtain. – Jakob Alexander Eichler Mar 17 '15 at 00:08
  • Yes, the questions is badly formulated that is because I'm not to goo in english. I will try to reformulate the questions. Sorry if I seam to abuse your expertise. I'm not. I tested both your answers but they are nod doing what I want. I will reformulate my question – Dan Mar 20 '15 at 14:31
  • No sweat. What directory is the .htaccess file in on your server? In relation to the web root directory? – bloodyKnuckles Mar 20 '15 at 14:36
  • I updated the questions. Now to answer your question I've put it in www.test.com and after that in www.test.com/shop too, but I think now that tokam was right. My questions was misleading and confusing. Please check the updated question. Thanks for your help – Dan Mar 20 '15 at 15:01
  • I read your updated question. Definitely more information. I updated my answer to exclude capturing the path. Okay, so you say the *.htaccess* has been tried in `/hsphere/local/home/danuser/test.com/shop`. Good. Another question: what directory is the *index.php* file in? – bloodyKnuckles Mar 20 '15 at 15:18
  • /hsphere/local/home/danuser/test.com/shop/index.php – Dan Mar 20 '15 at 15:29
  • Okay, so both *.htaccess* and *index.php* are in the `/hsphere/local/home/danuser/test.com/shop` directory. That's similar to how I set up my demo. Have you tried my updated answer? If that didn't work, what do your apache error logs report? – bloodyKnuckles Mar 20 '15 at 15:32
  • very close http://www.test.com/shop/ofno/?shopc=ddd works but http://www.test.com/shop/ofno/index.php?shopc=ddd does not work, goes to 404 – Dan Mar 20 '15 at 15:38
  • RewriteEngine On RewriteBase /shop RewriteRule ^.*$ index.php [L,QSA] – Dan Mar 20 '15 at 15:48
  • accept the edit so I can accept your answer as correct and thanks for your help – Dan Mar 20 '15 at 15:52