Apache rewrite - match query string - apache

I understand that using rewrites in apache if i want to match the query string then i should use
RewriteCond %{QUERY_STRING}
Which is fine, however, I have a csv with around 2000 urls to rewrite, a lot contain random query strings. It will be a painstaking process to go through each and create the rule.
Is there any generic way to have the rewrite look at the entire url, including the query string and redirect it?

try rewritemap
example config for vurtual host
RewriteEngine on
RewriteMap mymap "txt:/path/to/map.txt"
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteCond ${mymap:%1} >""
RewriteRule ^(.*)$ ${mymap:%1} [L]
and map.txt example
p=1&i=1 /test.php?n=2
p=1&i=2 /test.php?n=4
p=1&i=3 /test.php?n=6
...
Suppose that it is suitable for site with no hign load :)

Related

How to add redirects to pages with a query string?

I may be wrong in this but i'm seeing loads of answers for appending to a query string when using .htaccess for redirects but wondered if there was a clear way of achieving the following:
RewriteRule ^old-webpage/$ http://newwebsite.com/intro?redirect=oldsite [R=301,L]
So the old website doesn't have query strings and i'd like to be able to redirect people to the new site with a query string in place so I can adjust a few things and show a message.
This will work in redirecting, however now I'm getting the domain:
http://newwebsite.com/intro?redirect=oldsiteoldweb-page/
so is there something missing in the Rewrite rules that stops the old-webpage being concatenated to the query string after it's redirected?
Your above example didn´t work for my System so I suggest you following working conditions & rules:
Option 1 - dynamic param
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^(.*)$ http://newwebsite.com/intro?redirect=%{HTTP_HOST} [R=301,L]
Version 2 - static parameter
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^(.*)$ http://newwebsite.com/intro?redirect=oldsite [R=301,L]

.htaccess rewrite urls with parameters

I am trying to rewrite my urls through a .htaccess file to make them more clean looking. I have
http://localhost:801/Test/test.php?school=19&name=Greenhaven-Elementary
and it needs to end up looking like
http://localhost:801/Test/test.php/19/Greenhaven-Elementary
In my .htaccess file I have the following
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/+]+)([0-9]+)$ test.php?school=/$1&name=$2/ [L]
I have tried other ways but being new at using .htaccess files I haven't been able to figure it out.
This should do what you're after:
RewriteEngine On
RewriteCond %{QUERY_STRING} school=(.+)&name=(.+) [NC]
RewriteRule ^(.*)$ http://localhost:801/Test/test.php/%1/%2? [R=301,NC,L]
So what does the above do?
First, it will take the query school= and name= as a condition, if this condition is met then it will grab any version of the variables using (.+).
It will then rewrite the URL using 301 redirection to show http://localhost:801/Test/test.php/anything/anything2. The use of %1 and %2 is to grab the variables from school= / name= and then we use ? to stop the original query string from appearing on the end of the newly rewritten URL.
Make sure you clear your cache before testing this.
EDIT:
I wrote this for the singular query:
RewriteEngine On
RewriteCond %{QUERY_STRING} item=(.+) [NC]
RewriteRule ^(.*)$ http://localhost:801/Test/%1? [R=301,NC,L]
This includes removing test.php and on my server works without issue and returns http://localhost:801/Test/anything

apache query string rewrite rules

I am setting up Query string redirect :
expo.com/en/general/campaigns/on-second-thought.html?slide=ost-2016-tank to
expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html
RewriteCond %{HTTP_HOST} ^(.*)expo\.com
RewriteCond %{QUERY_STRING} slide=ost-2016-tank
RewriteRule  ^/en/general/campaigns/on-second-thought.html?$  http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html [R=301,L,NC]
redirect happening but its appending ?slide=ost-2016-tank like below
http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html?slide=ost-2016-tank
slide=ost-2016-tank parameter is added to redirected page
Since your rule does not define a new query string, the default behavior of Apache is to copy the old query string to the new URL. To get rid of it, append a ? to the address you rewrite/redirect to:
RewriteRule ^/en/general/campaigns/on-second-thought\.html?$ http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html? [R=301,L,NC]
Or, for Apache >= 2.4, you can also use the flag QSD (Query String Discard):
RewriteRule ^/en/general/campaigns/on-second-thought\.html?$ http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html [R=301,L,NC,QSD]
Simply add a blank query string when redirecting:
RewriteCond %{HTTP_HOST} ^(.*)expo\.com
RewriteCond %{QUERY_STRING} ^slide=(ost-2016-tank)$
RewriteRule ^(/?en/general/campaigns/on-second-thought)\.(html)$ $1/%1.$2? [R=301,L,NC]
No need to mention http://expo.com again when redirecting. It'll automatically redirect to the same hostname because of R flag. No need to repeat same strings over and over. Using match groups and referencing them later works.
Your pattern had .html?$ in it, which actually means that it'll match .html as well as .htm. You do not receive query strings in RewriteRule context.

rewrite apache httpd complete url with query string

I want to rewrite url from one application to another application of diferent path with query string using below code of lines in my httpd.conf
RewriteEngine On
RewriteRule ^/rforms/jsp/rform/index.jsp?(.*)$ /Project/jsps/rform/indexAIL.jsp?$1 [R]
RewriteRule ^/rforms/onlineLandingPage.do?(.*)$ /Project/onlineLandingPage.do?pid=1&loginType=2&$1 [R]
url redirects but query string param get missed.
Please suggest.
You cannot match query string in RewriteRule and there is no real need to match query string here since that will be carried over automatically.
Try these rules instead:
RewriteEngine On
RewriteRule ^/?rforms/jsp/rform/index\.jsp$ /Project/jsps/rform/indexAIL.jsp [NC,R,L]
RewriteRule ^/?rforms/onlineLandingPage\.do$ /Project/onlineLandingPage.do?pid=1&loginType=2 [R,L,NC,QSA]

removing directory in apache mod_rewrite

I have a PHP site which replaces an ASP site, so the path structure is different.
In the URLs, I need to match http://apache.site/Cartv3/Details.asp & redirect to another location. What is the correct syntax to match that URL fragment?
I've already tried
RewriteCond %{REQUEST_URI} CartV3/results1.asp?Category=60
RewriteRule ^(.*)$ home-study/A-Levels/1/page-1 [R=301,L]
and
RewriteRule ^CartV3/Details\.asp?ProductID=1004 home-study/A-Levels/1/page-1 [R=301,L]
You meed to read more about mod_rewrite. Remember RewriteRule doesn't match query string. You attempt needs to be rewritten as:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^Category=60$ [NC]
RewriteRule ^CartV3/results1\.asp$ /home-study/A-Levels/1/page-1? [R=302,L,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
PS: ? after page-1 is a special mod_rewrite syntax to strip original query string. If you want to keep original query string in rewritten URL then take out ? in the end.
The problem here is that you are trying to match the query string, which has to be done by a separate RewriteCond. If you want the match specifically "Category=60", then you can add it as a Condition:
RewriteCond %{QUERY_STRING} Category=60
RewriteCond %{REQUEST_URI} /CartV3/results1.asp
RewriteRule .* home-study/A-Levels/1/page-1?
This will match http://example.com/CartV3/results1.asp?Category=60 and redirect. The ? at the end of the rule stops "?Category=60" being to the resulting URI.
If you don't care about the value in the query string, then you can remove the first condition.