Solved

301 redirection help

Posted on 2016-08-17
9
43 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 77

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 77

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 77

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
The viewer will learn how to dynamically set the form action using jQuery.
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.

785 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