Solved

301 redirection help

Posted on 2016-08-17
9
45 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
  • 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
show child records separated by commas 12 34
Make login page safer 3 30
Converting PHP triple des decrypt to c# 9 62
Level out logo and increase size? 16 23
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
What You Need to Know when Searching for a Webhost Provider
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

679 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