.htaccess Rewrite Query String - .htaccess

I have troubles redirecting query strings and I really can't find a way how to do it. Let me be more specific:
I want to achieve the following: Redirect http://blablabla.com/default.asp?Kategori=1&Valg=77 to: http://blablabla.com/default_Kategori_1_Valg_77.html
Any help will be very appreciated.
Kind Regards,
Pavel Nikolaev

This should work :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^Kategori=1&Valg=77$ [NC]
RewriteRule ^default\.asp$ http://blablabla.com/default_Kategori_1_Valg_77.html? [L,R]

I would like to provide a new version of what starkeen advised me:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^Kategori=(.*)&Valg=(.*)$ [NC]
RewriteRule ^default\.asp$ http://blablabla.com/default_Kategori_%1_Valg_%2.html? [L,R]
The difference is that know I am catching the values of the Kategori and Valg parameters and use them in the new URL. Really interesting!
Thank you all.

Related

Multipule Query Strings .htaccess Redirect

Im a bit stuck where I need to redirect 80+ URLs' which have
content.php?111-rest-of-url
RewriteCond %{QUERY_STRING} ^?64-random-url-here-again$
RewriteRule ^content.php$ https://www.newdomain.com/random-url-here-again/? [R=301,L,QSD]
RewriteCond %{QUERY_STRING} ^?61-random-url-here$
RewriteRule ^content.php$ https://www.newdomain.com/random-url-here/? [R=301,L,QSD]
And there are around another 80 url's written out like this. However, when testing on site, no matter what URL you try...it always returns the first one in the .htaccess
From what I have been reading am I correct in assuming you can only use one query string of this kind ? if so is there away around this ?
Thanks in advance.
Ok so I managed to accomplish what I need by doing the following, hopefully it will help someone else.
RewriteCond %{REQUEST_URI} ^/content\.php$
RewriteCond %{QUERY_STRING} ^196-random-url-here-again$
RewriteRule ^(.*)$ https://www.newdomain.com/random-url-here-again/? [R=301,L,QSD]
RewriteCond %{REQUEST_URI} ^/content\.php$
RewriteCond %{QUERY_STRING} ^196-random-url-here$
RewriteRule ^(.*)$ https://www.newdomain.com/random-url-here/? [R=301,L,QSD]
Not sure what the difference is but it works :), would be grateful to hear from someone to explain the difference in what I have done so I understand it in the future.

htaccess redirect to certain page depending on condition

I have the following urls:
www.mydomain.com/contacto.html
www.mydomain.com/pedidos.html
www.mydomain.com/quienes.html
I want to redirect them to:
www.mydomain.com/
I have wrote the following script, but it is not working ok.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(contacto\.html|pedidos\.html|quienes\.html)$
RewriteRule ^ / [R=301,L]
Do you have any idea how to make this thing working?
Thanks in advance,
Emanuel
This is not the first time I see the problem here. I do not know why.
But I know it works like that:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(contacto\.html|pedidos\.html|quienes\.html)$
RewriteRule ^ http://www.domain.com/ [R=301,L]
Delete your cache or try from other browser if it does not work the first time.
Thanks Croises,
I've used something very similar to what you has suggested.
I did the following:
RewriteCond %{REQUEST_URI} ^/(home|pedidos|quienes|contacto).html$
RewriteRule .* / [R=301,L]
Thank you very much for your help.
BR

rewrite rules for utm_source parameter in url

guys, i have question on mod_rewrite function.
this is outside link like
http://example.com/article?utm_sources=baidu&utm_campain=alading
I like to change it to
http://example.com/article#utm_sources=baidu&utm_campain=alading
#utm_sources=xxx also can be traced by Google GA code
I try below code but not working
RewriteCond %{QUERY_STRING} ^utm_source(&.*)?$ [NC]
RewriteRule ^article$ /article?%1 [R=301,NE,NC,L]
how do i make rewrite rules for it? thanks
You can use this rule:
RewriteCond %{QUERY_STRING} ^utm_sources=.+ [NC]
RewriteRule ^article/?$ /article#%{QUERY_STRING}? [R=301,NE,NC,L]
Trailing ? is required to strip off previous query string.
I update with below rules it works for me, but thanks anubhava give answer.
RewriteCond %{QUERY_STRING} ^utm_(.*)?$
RewriteRule ^(.*)$ %{REQUEST_URI}#%{QUERY_STRING}? [R=301,NE,NC,L]

Passing a Query String via .HTACCESS

I have a dynamic query string that I need to pass via an .htaccess redirect. For example:
I need to redirect this URL: http://mysite.com/page1?action=signup&var2=dynamicVar
To this: http://mysite.com?action=signup&var2=dynamicVar
I know this is pretty simple, but I'm really not sure what type of rule/syntax would work for this.
Any help is greatly appreciated!
If you already have .htaccess then simply add this line:
RewriteRule ^page1/?$ page2 [L,R,QSA,NC]
Update: Based on your comments:
RewriteCond %{QUERY_STRING} (^|&)action=signup(&|$) [NC]
RewriteRule ^page1/?$ / [L,R,QSA,NC]
RewriteRule ^page1(.*)$ /page2$1 [L,R=301]
Input
http://mysite.com/page1?action=signup&var2=dynamicVar
this is the rewrite rule
RewriteRule ^page1?(.*)$ /?$1 [L,R=301]
redirect to
http://mysite.com/?action=signup&var2=dynamicVar
this will redirect all request o page1 with get parameters to http://mysite.com
Use %{REQUEST_URI}
fi.:
RewriteCond %{HTTP_HOST} ^website\.(.+)$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
rewrites anything that starts with "website" to "www.website..." including the querystring (by use of %{REQUEST_URI})

how to write a .htaccess redirect like stackoverflow does for its questions

I'm trying to write a .htaccess rule that would redirect someone asking for
http://mysite.com/questions/123/my-question-name
to
http://mysite.com/questions/question_handler.php?qid=123
Here's what i wrote so far (it's not working):
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?(.*)$ [NC]
RewriteRule ^(.*)/questions/(\d+)/(.*)$ http://%1/questions/question_handler.php?qid=%2$1 [R=301,L]
Any help is much appreciated.
RewriteRule ^http://([^/]*)/questions/(\d+)/(.*)$ http://$1/questions/question_handler.php?qid=$2
Your (.*) was probably too greedy so it was using http://mysite.com/questions/123/my-question-name as the first group matched
I would do something like this
RewriteEngine on
RewriteRule ^questions/([0-9]+)/?$ questions/question_handler.php?qid=$1 [NC,L]

Resources