Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

301 redirection help

Posted on 2016-08-17
9
Medium Priority
?
58 Views
Last Modified: 2016-09-07
Hello

I am trying to forward a specific request to my home page using .htaccess and 301 redirects but I am having problems and I am not sure why.

I am using OpenCart and are using the product tags for admin use but not in the front end. I want to forward any request for the tags to the homepage. An example string would be "/index.php?route=product/search&tag=small" - So if the url contains "product/search&tag=" then I want it to forward to /

I have tried a number of things but have not been able to get it to work. Including

RewriteRule (.*)product/search&tag=(.*) / [R=301,L] 

Open in new window


RewriteRule   ^/index.php?route=product/search&tag=(.*)  /  [R=301,L]

Open in new window


But I cannot get it to work.

Can someone please advise where I am going wrong?

Many Thanks
Adam
0
Comment
Question by:Adam Jones
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 79

Assisted Solution

by:arnold
arnold earned 1600 total points (awarded by participants)
ID: 41760309
Question is whether you are first, have the setting to allow processing of .htaccess. http.conf AllowOverride
Is rewrite enabled.
The second part is whether you are placing the directive in the correct path. i.e. is the .htaccess where you have the rewrite is in the sme location where index.php is.

The other part, rewrite has an option to enable tracing where you can configure the engine to record when a match is considered and if matched/or not will reflect the URL in the consideration.
0
 
LVL 51

Expert Comment

by:Steve Bink
ID: 41760326
Also, those parameters are typically part of the query string.  Your example did not show a '?' after the URL, so it is a little unclear.  Can you paste the exact URL?
0
 

Author Comment

by:Adam Jones
ID: 41760382
Yes rewrite is enabled. It is working fine with a number of other queries.

the full url would be : https://www.website.com/index.php?route=product/search&tag=small
I want that to forward to: https://www.website.com
I can do this with

RewriteCond %{QUERY_STRING} (^|&)route=product/search($|&)
RewriteCond %{QUERY_STRING} (^|&)amp;tag=small($|&)
RewriteRule ^index\.php$ /? [L,R=301]

Open in new window


I would also like anything instead of small to forward to website.com (so index.php?route=product/search&tag=big or index.php?route=product/search&tag=red)

I am not that great with the .htaccess using variables and wilecards ect. but I am trying to learn, so I am asking some advice on what I should put for the line?
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 79

Accepted Solution

by:
arnold earned 1600 total points (awarded by participants)
ID: 41760471
First thing is to confirm that your webserver allows these directives in the .htaccess

Here is the rewrite (moid_rewrite where you can add tracing.

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

You would only surround your directives within .htaccess  to record provided your apache version is newer.

Anyreason/'option to add this feature to index.php.

Check QUERY-STRING ......
0
 
LVL 51

Expert Comment

by:Steve Bink
ID: 41760566
I agree with arnold - strongly consider putting this "redirection" in your application.  Based on your query string variables, I'm assuming you already have a router.  The .htaccess file is not the ideal place for application logic.

What is the specific logic you want to use for the redirect?  For example, the earlier bit was
if query string contains "route=product/search" and also contains "&tag=small", then redirect to "/" and erase the query string
0
 

Author Comment

by:Adam Jones
ID: 41760810
After some research I have done it like this and it is working perfectly.

RewriteCond %{QUERY_STRING} (^|&)route=product/search($|&)
RewriteCond %{QUERY_STRING} (^|&)amp;tag=(.*)$
RewriteRule ^index\.php$ /? [L,R=301]

Open in new window


I understand what you are suggesting by putting it in the application but I do not see why this would be any better than what I have done in the .htaccess.
0
 
LVL 79

Assisted Solution

by:arnold
arnold earned 1600 total points (awarded by participants)
ID: 41760893
The processing involved. within index.php it will evaluate the rule only when index.php is accessed, within .htaccess the webserver evaluates the directives on every access of any file within the directory where the .htaccess is.
i.e. for every redirect it evaluates it twice once where index.php?product is the request and the second time after the redirect
0
 
LVL 40

Assisted Solution

by:noci
noci earned 400 total points (awarded by participants)
ID: 41761627
a ? in a Regex means the match (a p here) is optional...

so re RE's did match index.phroute  
or  index.phproute...

And anything behind a ? in q URL is a QUERY_STRING as has been pointed out before
0
 
LVL 40

Expert Comment

by:noci
ID: 41787473
Arnold suggestions are the right way to go,  Noci explains why the regex fails.
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question