Solved

301 redirection help

Posted on 2016-08-17
9
38 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 76

Assisted Solution

by:arnold
arnold earned 400 total points (awarded by participants)
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 76

Accepted Solution

by:
arnold earned 400 total points (awarded by participants)
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 50

Expert Comment

by:Steve Bink
Comment Utility
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
Comment Utility
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 76

Assisted Solution

by:arnold
arnold earned 400 total points (awarded by participants)
Comment Utility
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 39

Assisted Solution

by:noci
noci earned 100 total points (awarded by participants)
Comment Utility
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 39

Expert Comment

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

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now