htaccess rule to remove parameters - apache

struggling to come up with a .htaccess rule to remove parameters from it.
Essentially I want
http://www.example.com/page/competition.html?utm_medium=email&utm_campaign=11&utm_source=11&utm_term=11.gif
to rewrite to
http://www.example.com/page/competition.html
so, anything right from the /page/compeition.html is rewritten.
Grateful for any assistance. thanks in advance

In your question, you state that you want the long URI rewritten to the shorter one. Unfortunately, that isn't very clear. Assuming that you wish to simply remove the query string using a redirect, you can use the following:
RewriteCond %{QUERY_STRING} ^utm_medium=email&utm_campaign=([^&]+)&utm_source=([^&]+)&utm_term=([^&]+)$
RewriteRule ^ %{REQUEST_URI}? [R=302,L]
This is a generalised condition and rule that will check for any utm information in the query string. If it is present, then strip out the query string.
If you wish to only do this for page/competition.html, then change the rule to this:
RewriteRule ^(page/competition\.html)$ /$1? [R=302,L]
If you wish to simply remove any query string forming part of a request to page/competition.html, then you can use this instead of the above:
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(page/competition\.html)$ /$1? [R=302,L]
This basically tells Apache to check if the query string is not empty and, if it isn't, strip it out altogether.
To make the redirect permanent, change 302 to 301.

As a general rule, you should be able to use the following:
RewriteRule ^(.*)$ $1? [END,QSA]
The key in this rule is the trailing questionmark which will not appear in the output URL.
RewriteRule ^(.*)$ $1? [END,QSA] This rule was met, the new url is
http://www.example.com/page/competition.html
Try it out at the following:
http://htaccess.madewithlove.be/

Do you want that only for the specific page? Or more as a general approach?
RewriteRule ^page/competition.html?(.+)$ /page/competition.html [L,NC]
That would be the very easiest form to achieve it, but would only work for the exact filename. For a more general approach, you would need to add some more placeholders.

Related

Apache Mod_Rewrite empty query string followed by slash

I have some super weird URLs I need to redirect. The original URLs look like this:
server1.com/directory?/tfoo
These URLs needs to go here:
server2.com/search~?query=foo
I've tried a bunch of different possibilities, but this is what I'm working with right now:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/t(.*) https://server2.com/search?query=$2 [L,NE,NC]
Basically, I thought I would have to look for a blank query string, and then try to move on to grab the value given in the directory structure (foo). The RewriteCond matches my original URL, but I can't seem to grab the actual query parameter.
I've tried a bunch of things with RegEx to try to ignore the questionmark altogether, but it looks like mod_rewrite only understands the questionmark in the context of a query parameter redirect.
Any advice is hugely appreciated.
Thanks!
You can use this :
RewriteEngine on
RewriteCond %{THE_REQUEST} /directory/?/t(.+)\s [NC]
RewriteRule ^ http://server2.com/search~?query=%1 [NE,L,R]
Deadooshka's solution (commented above) worked:
RewriteCond %{QUERY_STRING} "^/t(.*)$"
RewriteRule "^/?directory$" "https://server2.com/search?query=%1" [L,NE,NC]`

How to create an htaccess rewrite rule with part of parameter as identifier

I need some help with a rewrite rule I am struggling with.
I have the existing rule below, which works well and redirects as follows:
www.site.com/page.php?type=1&category=2&cond=3
redirected to
www.site.com/1/2/3/
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /page.php?type=$1&category=$2&cond=$3 [L]
Now I need to create one new rule that will not interfere with the rule above.
So this url:
www.site.com/page.php?type=1&category=2&page=page-1
should redirect to:
www.site.com/1/2/page-1/
Obviously, the browser should make somehow the difference between www.site.com/1/2/3/ and www.site.com/1/2/page-1/.
This difference can be the part of the third parameter which will be always constant: page- .
I know I need to somehow modify this part of the new rule ([^/]*) but nothing I have tried so far does to job.
Any suggestions how should I accomplish this?
Have these 2 rules in this order:
RewriteRule ^([^/]+)/([^/]+)/(page-[^/]+)/?$ /page.php?type=$1&category=$2&cond=$3 [L,QSA,NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /page.php?type=$1&category=$2&cond=$3 [L,QSA]
You should be able to do your rules this way.
RewriteRule ^([^/]*)/([^/]*)/page-([^/]*)/?$ /page.php?type=$1&category=$2&page=page-$3 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /page.php?type=$1&category=$2&cond=$3 [L]

Redirect rewrite rule, Adding parameter at the end of url

If /c/ is part of URL parameter then I want to add parameter at the end of URL parameter. because parameter may increase or decrease.
http://example.com/c/file.php?par1=val1&par2=val2
I need add two parameter &addpar1=val&addpar2=val at the end of URL like this.
http://example.com/c/file.php?par1=val1&par2=val2&addpar1=val&addpar2=val
What I am trying to do here:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/c/(.*)$ [NC]
RewriteRule /c/ /%1 [QSA]
Please suggest me what should written in RewriteRule here.
Your rule is close, but you're not actually adding anything to the query string. Try:
RewriteEngine On
RewriteCond %{QUERY_STRING !&addpar1=val&addpar2=val
RewriteRule ^/?c/(.*)$ /%1?%{QUERY_STRING}&&addpar1=val&addpar2=val [L]
Here, you need to check that the parameters has already been added, then you add them to the end of the query string. You don't want the QSA flag here because you're manually doing the appending.
If you want to redirect the browser so that they see the query strings then you need an R or R=301 flag in the square brackets (separated by a comma).

Rewrite rule on multiple variables using Apache mod_rewrite

I would like to know how can I rewrite www.site.com?lang=lv&type=1&mode=2 into www.site.com/lv/1/2 using Apache mod_rewrite options.
Assuming that you actually want to rewrite /lv/1/2 to ?lang=lv&type=1&mode=2 (I see no reason to do the opposite) and that no other rewrites are active, this should do the trick:
RewriteRule ^/([^/]*)/([^/]*)/([^/]*)$ ?lang=$1&type=$2&mode=$3 [L]
Also; you'd be better off replacing those magic numbers with more useful information if you want to include them in your URI.
Edit: If it really is the opposite you'd like to do, see the answer by Matt S.
Basic rewrite:
RewriteEngine On
RewriteRule http://www.site.com/?lang=(.+?)&type=(\d+?)&mode=(\d+?) http://www.site.com/$1/$2/$3 [L,R=permanent]
Edit: Changed rewrite flags to force a permanent redirect
You need to handle the URI path and query separately. If the parameters appear in that very same order, you can do this:
RewriteCond %{QUERY_STRING} ^lang=([^&]+)&type=([^&]+)&mode=([^&]+)$
RewriteRule ^$ /%1/%2/%3? [L,R=301]
Otherwise you will need to put them in the right order before using this rule or take each parameter at a time.

mod_rewrite weird problem

I have a strange problem with mod_rewrite, the rules that are relevant here are:
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/igre\-(.*)\.php$ game.php?GameUrl=$2&Page=1 [L]
And a corresponding URL might look something like this:
example.com/miselne-igre/igre-shirk.php?Page=2
example.com/miselne-igre/igre-shirk.php
The problem is that the first rule has no effect. If I use the first URL from the example I always get 1 into the Page variable, which shows that the second rule is used.
So what's wrong with the first one? And why is the second rule even matching a URL with ".php?Page=XYZ" at the end, if I said that the URL ends with ".php"?
ps: other rules in the .htaccess file are working fine...
The query string is not part of the URI path that is being processed by the RewriteRule directive. You have to use the RewriteCond directive to process the query string.
RewriteCond %{QUERY_STRING} ^Page=[0-9]+$
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&%0 [L]
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&Page=1 [L]
But you can still simplify this by using the QSA flag (query string append):
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1 [L,QSA]
mod_rewrite is not using the query in it's rewriting process. Therefor you first RewriteRule is ignored. You could combine it with a RewriteCond (haven't tested it though) like so:
RewriteCond %{QUERY_STRING} Page=([0-9]+)
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2 [L, qsappend]
# qsappend appends the original query, in this case (Page=xx)
Ah, like Gumbo said; you can also use %1 to back reference to the page numer.
Is it just me or are your arguments back-to-front?
Do you mean:
RewriteRule ^(.*)\/(.*)\-igre\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/(.*)\-igre\.php$ game.php?GameUrl=$2&Page=1 [L]
You wanted to match miselne-igre not igre-miselne.
Obviously this doesn't address the main issue, but thought I'd throw that in.
Dom