Solved

301 redirection help

Posted on 2016-08-17
9
46 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 78

Assisted Solution

by:arnold
arnold earned 400 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 50

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
Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

 
LVL 78

Accepted Solution

by:
arnold earned 400 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 50

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 78

Assisted Solution

by:arnold
arnold earned 400 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 100 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

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I allow for non standard CSV file 9 45
Wordpress contact form sending spam 2 51
Securing source code... theoretical question 10 48
Link Stopped Working 7 31
What You Need to Know when Searching for a Webhost Provider
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 learn how to count occurrences of each item in an array.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

734 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