0

Trying to redirect:

/category/unique-name?foo=123&bar=&abc=

to:

http://www.example.com/new/page

The majority of the articles I've read hint towards redirecting a query string pattern for multiple pages. I'm just after the one page that needs a redirect.

My latest attempt looks like this:

    RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^/category/unique-name/$
    RewriteCond %{QUERY_STRING} ^foo=123&bar=&abc=$
    RewriteRule ^(.*) $ http://www.example.com/new/page? [R=301,L]

Any help greatly appreciated!

UPDATE 07/04/2017

Thanks for all of the suggestions. I've tried them all, some resulted in an Error 500, some had no effect. I've listed which are which below...

# NO CHANGE

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/category/unique-name$
RewriteCond %{QUERY_STRING} ^foo=123&bar=&abc=$
RewriteRule ^ http://www.example.com/new/page [R=301,L]

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/category/unique-name$
RewriteCond %{QUERY_STRING} ^foo=123&bar=&abc=$
RewriteRule ^ http://www.example.com/new/page [R=301,L]

# ERROR 500

RewriteEngine On
RewriteCond %{QUERY_STRING} ^foo=123&bar=&abc=$
RewriteRule ^/?category/unique-name/?$ $ http://www.example.com/new/page? [R=301,L]

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/category/unique-name/?$
RewriteCond %{QUERY_STRING} ^foo=123&bar=&abc=$
RewriteRule ^ $ http://www.example.com/new/page [R=301,END,QSD]

RewriteEngine On
RewriteCond %{QUERY_STRING} ^foo=123&bar=&abc=$
RewriteRule ^/?category/unique-name/?$ $ http://www.example.com/new/page [R=301,END,QSD]
4
  • I tried the suggestions below and updated the initial post with my results ^^^ Commented Apr 8, 2017 at 0:47
  • "didn't work" does not help, you will have to tell us more details. 1. please post the exact URL requested that lead to your results from your http servers access log file, 2. post the entries you receive in the http servers error log file in case you receive an http status 500. Commented Apr 10, 2017 at 6:45
  • Also please specify what version of the http server you actually operate. I suspect it is a very old one. Commented Apr 10, 2017 at 6:57
  • I ask about the specific URLs (not replacements) since all this looks like there is no real issue with the suggestions, but with the specific, real world data you are working with. Commented Apr 10, 2017 at 6:59

2 Answers 2

1

Try it like this,

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/category/unique-name$
RewriteCond %{QUERY_STRING} ^foo=123&bar=&abc=$
RewriteRule ^ http://www.example.com/new/page [R=301,L]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion but unfortunately it didn't work.
After some trying, it worked for me, but I had to do two steps: 1) put the code at the start of the htaccess; 2) put a ? after the example.com/new/page otherwise the query string will be added again to the URL and it will cause a redirect loop
0

Your attempt is close to perfect. Have a try with one of these two only slightly modified alternatives:

Approach 1 (old):

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/category/unique-name/?$
RewriteCond %{QUERY_STRING} ^foo=123&bar=&abc=$
RewriteRule ^ $ http://www.example.com/new/page? [R=301,L]

Approach 2 (old):

RewriteEngine On
RewriteCond %{QUERY_STRING} ^foo=123&bar=&abc=$
RewriteRule ^/?category/unique-name/?$ $ http://www.example.com/new/page? [R=301,L]

In case you operate a not too outdated version of the apache http server you can implement a slightly cleaner final URL:

Approach 1 (new):

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/category/unique-name/?$
RewriteCond %{QUERY_STRING} ^foo=123&bar=&abc=$
RewriteRule ^ $ http://www.example.com/new/page [R=301,END,QSD]

Approach 2 (new):

RewriteEngine On
RewriteCond %{QUERY_STRING} ^foo=123&bar=&abc=$
RewriteRule ^/?category/unique-name/?$ $ http://www.example.com/new/page [R=301,END,QSD]

If these lead to an http status 500 (internal server error), then chances are that indeed your version of the http server is too old and you have to stick to the work around shown in the first two approaches.


However looking at the fact that you implement an external redirection (R=301) I wonder if that will work and actually implements what you are trying to achieve...

Your own attempt (and the modified version above) rewrite an incoming request with those get parameters externally to the clean URL. So they change the URL visible in the browser. Which is pretty and fine, but: Is your setup actually able to process the next incoming request to /new/page?


And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

1 Comment

Thanks for the suggestions but unfortunately they didn't work. I've updated my post with the results from each of your options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.