RewriteCond %{QUERY_STRING} in mod_rewrite (dynami

2019-09-20 17:26发布

As I have encountered very luck with managing to get querystrings to redirect correctly previously just passing querystring parameters, and the over-arching advice across this site and webmasterworld for querystring redirection seems to be "deal with it as a RewriteCond querystring", I'm trying to use the following type rule for a set of about 10 URLs.

Example URL:

http://www.example.org/training_book.asp?sInstance=1&EventID=139

What I have so far:

RewriteCond %{QUERY_STRING} ^training_book.asp\?sInstance=1&EventID=139  
RewriteRule /clean-landing-url/ [NC,R=301,L]

So, what I want to happen is

http://www.site.org/training_book.asp?sInstance=1&EventID=139 301> http://www.site.org/clean-landing-url

but instead what is happening is this:

http://www.site.org/training_book.asp?sInstance=1&EventID=139 301> http://www.site.org/training_book.asp/?sInstance=1&EventID=139

It's appending a forward slash just before the querystring, and then resolving the full URL (obviously, 404ing.)

What am I missing? Is it a regex issue with the actual %{QUERY_STRING} parameter?

Thanks in advance!

EDIT -

Here's where I am so far.

Based upon the advice from @TerryE below, I've tried implementing the following rule.

I have a set of URLs with the following parameters:

http://www.example.org/training_book.asp?sInstance=1&EventID=139
http://www.example.org/training_book.asp?sInstance=2&EventID=256
http://www.example.org/training_book.asp?sInstance=5&EventID=188

etc.

which need to redirect to

http://www.example.org/en/clean-landing-url-one
http://www.example.org/en/clean-landing-url-two
http://www.example.org/en/clean-landing-url-three

etc.

This is the exact structure of the htaccess file I have currently, including the full examples of the "simple" redirects which are presently working fine (note - http://example.com > http://www.example.com redirects enforced in httpd.conf)

#301 match top level pages
RewriteCond %{HTTP_HOST} ^example\.org [NC]
RewriteRule ^/faq.asp /en/faqs/ [NC,R=301,L] 

All URLs in this block are of this type. All these URLs work perfectly.

#Redirect all old dead PDF links to English homepage.
RewriteRule ^/AR08-09.pdf /en/ [NC,R=301,L]

All URLs in this block are of this type. All these URLs work perfectly.

The problem is here: I still can't get the URLs of the below type to redirect. Based upon advice from @TerryE, I attempted to change the syntax as below. The below block does not function correctly.

#301 event course pages
RewriteCond %{QUERY_STRING} sInstance=1EventID=139$
RewriteRule ^training_book\.asp$ /en/clean-landing-url-one? [NC,R=301,L]

The output of this is

http://staging.example.org/training_book.asp/?sInstance=1&EventID=139

(this is currently applying to staging.example.org, will apply to example.org)

(I had "hidden" some of the actual syntax by changing it to event_book from training_book in the initial question, but I've changed it back to be as real as possible.)

1条回答
一纸荒年 Trace。
2楼-- · 2019-09-20 18:02

The the documentation. QUERY_STRING contains the request content after the ?. Your condition regexp should never match. This makes more sense:

RewriteCond %{QUERY_STRING}   ^sInstance=1&EventID=139$ 
RewriteRule ^event_book\.asp$ /clean-landing-url/ [NC,R=301,L]

The forward slash is caused by a different Apache filter (DirectorySlash).

查看更多
登录 后发表回答