ISAPI Rewrite : Append querystring to the rewrited URl's Query String - isapi-rewrite

Actually, using the following ISAPI rule
RewriteCond %{HTTP:Host} ^domain.com$
RewriteRule ^/Product-name/$ /test.cfm?ProductID=xxxx [NC,L]
I'm rewriting the following URL
htttp://www.domain.com/Product-name/
to
htttp://www.domain.com/test.cfm?ProducID=xxxx
This is working fine, But when I use the querystring in URL, it is not working
For Egs:
The following URL is not working
htttp://www.domain.com/Product-name?categoryID=YYYY
I need to rewrite the above URL as follows
htttp://www.domain.com/test.cfm?ProducID=xxxx&categoryID=YYYY
I've used the following rule, but no luck
RewriteCond %{QUERY_STRING} ^param=(\d+)$ [NC]
RewriteRule ^/Product-name/$ /test.cfm?ProductID=xxxx?param2=%1? [NC,L]
So is there any solution for this?

If you are using ISAPI_Rewrite 3, then you should add QSA flag to append original query string to the resulting URL:
RewriteRule ^/Product-name/$ /test.cfm?ProductID=xxxx [NC,L,QSA]

The following rule, appends the query string to the rewritten URL's Querystring and avoids the 404 error
URL With Querystring and Trailing slash(/)
RewriteRule ^/Product-name/\? /test.cfm?ProductID=xxxx [NC,L,QSA]
URL With Querystring and Without Trailing slash(/)
RewriteRule ^/Product-name\? /test.cfm?ProductID=xxxx [NC,L,QSA]
URL Without Querystring and with Trailing slash(/)
RewriteRule ^/Product-name/$ /test.cfm?ProductID=xxxx [NC,L]
URL Without Querystring and Trailing slash(/)
RewriteRule ^/Product-name$ /test.cfm?ProductID=xxxx [NC,L]

Related

Rewrite rule on URL query string

Can somebody help me how to replace query string URL. I want to redirect URL (my requirement to remove %2Fcontent%2Fabc from query string URL).
From :
http://something.com/en-US/login.html?resource=%2Fcontent%2Fabc%2Fen-US%2Fxyz.html&$$login$$=%24%24login%24%24
to:
http://something.com/en-US/login.html?resource=%2Fen-US%2Fxyz.html&$$login$$=%24%24login%24%24
I tried using rewrite condition and rule below but no luck.
RewriteCond %{QUERY_STRING} ^resource=%2Fcontent%2Fabc(.*)$
RewriteRule ^/(.*)?resource=%2Fcontent%2Fabc(.*)$ /$1?resource=%2 [L]

Rewrite URL so that query string is in path

I'm trying to get the following path: /faculty/index.php?PID=FirstLast&type=alpha
To rewrite to this: /faculty/FirstLast
Am I correct to assume the following would be acceptable to put in .htaccess?
# Rewrite old URLS
RewriteCond %{QUERY_STRING} ^PID=([0-9a-zA-Z]*)$
RewriteRule ^/faculty/index.php$ /faculty/%1 [R=302,L]
I'm okay to throw away any other query string variables. I'm applying these rules at the .htaccess file level. This project is a migration from an older system into Drupal.
Outcome:
My .htaccess looks like
# Rewrite old URLS
RewriteCond %{QUERY_STRING} PID=([0-9a-zA-Z]*)
RewriteRule ^faculty/ /faculty/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} vidID=([0-9]*)
RewriteRule ^videos/ /video/id/%1/? [R=301,L]
I also found this wonderful tool -- a mod_rewrite tester
http://htaccess.madewithlove.be/
All good!
Try this instead:
RewriteRule ^faculty/index.php$ /faculty/%1? [R=302,L]
The leading slash is not in the URI-path tested in the rule, so can't be in the regex either.
As the query is automatically appended to the substitution URL (passed through unchanged) unless a new query is created in the rule, the trailing question mark ? erases the existing query string when the rule is used.

mod_rewrite to redirect URL with query string

I have a lot of working mod_rewrite rules already in my httpd.conf file. I just recently found that Google had indexed one of my non-rewritten URLs with a query string in it:
http://example.com/?state=arizona
I would like to use mod_rewrite to do a 301 redirect to this URL:
http://example.com/arizona
The issue is that later on in my rewrite rules, that 2nd URL is being rewritten to pass query variables on to WordPress. It ends up getting rewritten to:
http://example.com/index.php?state=arizona
Which is the proper functionality. Everything I have tried so far has either not worked at all or put me in an endless rewrite loop. This is what I have right now, which is getting stuck in a loop:
RewriteCond %{QUERY_STRING} state=arizona [NC]
RewriteRule .* http://example.com/arizona [R=301,L]
#older rewrite rule that passes query string based on URL:
RewriteRule ^([A-Za-z-]+)$ index.php?state=$1 [L]
which gives me an endless rewrite loop and takes me to this URL:
http://example.com/arizona?state=arizona
I then tried this:
RewriteRule .* http://example.com/arizona? [R=301,L]
which got rid of the query string in the URL, but still creates a loop.
Ok, adding the 2nd RewriteCond finally fixed it - now rewriting correctly and no loop:
# redirect dynamic URL: ?state=arizona
RewriteCond %{QUERY_STRING} state=arizona [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule .* http://domain.com/arizona? [R=301,L]
# older rewrite rule that passes query string based on URL:
RewriteRule ^([A-Za-z-]+)$ index.php?state=$1 [L]
And here's the code to make it work for any state value, not just arizona:
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{QUERY_STRING} ^state=([A-Za-z-]+)$ [NC]
RewriteRule .* http://domain.com/%1? [R=301,L]
RewriteCond %{REQUEST_URI} ^/\?state=arizona$ [NC]
should be
RewriteCond %{QUERY_STRING} state=arizona [NC]
The request_uri ends at the ? token.

How to forward one url to another using mod_rewrite rules

Im trying to forward this url, but its not working - here is my code:
RewriteRule ^https://www.wsjwine.com/discovery_offer.aspx?promo=2227006$ https://www.wsjwine.com/discovery_offer_lp2.aspx?promo=2227006 [L]
With RewriteRule directive you can only test the URL path. For further tests you need to use additional RewriteCond directives.
Now if you want to rewrite every request of /discovery_offer.aspx to /discovery_offer_lp2.aspx regardless of how the query looks like, you can just use this (example for the .htaccess file in the root directory):
RewriteRule ^discovery_offer\.aspx$ discovery_offer_lp2.aspx [L]
If you don’t specify a query in the substitution, the originally requested query is automatically appended to the new one.
And if you just want to rewrite that specific URL, try this:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} =www.wsjwine.com
RewriteCond %{QUERY_STRING} =promo=2227006
RewriteRule ^discovery_offer\.aspx$ discovery_offer_lp2.aspx [L]
You can't detect query strings like that. Use RewriteCond %{QUERY_STRING}.

help rewriting this url. simple but not working

RewriteCond %{REQUEST_URI} !^/?ping.php
RewriteRule ^/\?(.*)$ ping.php?url=$1 [L]
i am trying to match any character that follows /?
www.mysite.com/?someurl.com
instead it keeps loading the index.html !
You cannot match the query string with mod_rewrite, but if you still want to pass it on, you can add %{QUERY_STRING} to the resultant url, for example
RewriteRule ^.*$ ping.php?url=%{QUERY_STRING} [L]

Resources