Rewrite URL removing all after domain name - apache

i need rewrite all URL of specific domain to other exact URL without parameters, all URL old.domain.com/* to new.domain.com
I have set this rule into .htaccess
RewriteCond %{HTTP_HOST} ^old.domain.com [OR]
RewriteCond %{HTTP_HOST} ^www.old.domain.com
RewriteRule ^(.*)$ https://new.domain.it [R=permanent,L]
so, if i go to
old.domain.com/foo or
old.domain.com/foo/bar or
old.domain.com/foo/bar/index.php
it work perfectly, but if i go to old.domain.com/index.php?goofy, it redirect to new.domain.com/?goofy, i can redirect always to new.domain.com?

When the url is old.domain.com/index.php?goofy Mod-rewrite appends ?goofy to its destination url. This is because mod-rewrite appends query string (Url part after the ? character) to the redirected url .
If you are on Apache 2.4 you can use QSD Query string Discard flag to remove old query string from new url.
RewriteRule ^(.*)$ https://new.domain.it [R=permanent,L,QSD]
If your apache version is below 2.4 ,simply add an empty question mark ? at the end of the target url to remove old query string.
RewriteRule ^(.*)$ https://new.domain.it/? [R=permanent,L]

Related

htaccess redirect to exclude certain pages

My htaccess file currently redirects everything and has this
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.co.uk/$1 [R,L]
I need to exclude two urls that begin with "send"
I changed the last line to
RewriteRule !^send(.*) https://www.domain.co.uk/$1 [R,L]
It excluded the send urls but any url in a subfolder is redirected to the root index page.
RewriteRule !^send(.*) https://www.domain.co.uk/$1 [R,L]
Negated patterns don't capture anything (by definition), but trying to capture everything after send when send is not present in the URL, doesn't make much sense.
You can do something like the following and use the REQUEST_URI server variable in the substitution instead of the backreference:
RewriteRule !^send https://www.domain.co.uk%{REQUEST_URI} [R,L]
Note that the REQUEST_URI server variable already contains the slash prefix.

Redirecting without query

I want to set redirection form ^mysite/ to www.mynewsite.example.
The problem is I don't want this direction when is www.mysite.example?test=test.
Redirection should works when client goes by www.mysite.example.
I tried to use RewriteRule
^mysite.xhtml$ www.mynewsite.example [L,R=301,NC]
But it still doesn't work
RewriteRule ^mysite.xhtml$ www.mynewsite.example [L,R=301,NC]```
I don't want this direction when my URL is www.mysite.example?test=test
You can use the following rule to stop the url redirection when your root url contains ?test=test querystring
RewriteEngine on
#if not ?test=test querystring
RewriteCond %{QUERY_STRING} !^test=test$ [NC]
#then Redirect the root url to www.newdomain.example
RewriteRule ^/?$ http://www.newdomain.example/ [L,R]
You can replace test=test with test=.+ in the RewriteCond above if it's a dynamic value.
^/?$ matches your root URI ie. / . If you want to redirect the entire site then replace it with ^.*$ .

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.

How to use htaccess Rewrite rule for just part of a url

I am trying to create a rewrite rule in my httacess file for part of the url. I want to rewrite the url if the request contains a specific string. For example change the url only if contains /members/
So
mydomain.com/members/
mydomain.com/members/activity/ros1...
mydomain.com/members/ay/bd...
ALL above should change to another url because matches /members/ string in the url
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET /members/(.*)
RewriteRule ^(.*)$ - [F,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/members(.*)
RewriteRule ^(.*)$ - [F,L]
I have tried various combinations but does not seem to work. I'm sure I'm doing something really wrong as not on expert on this. Appreciate any pointers.
RewriteCond %{REQUEST_URI} ^/members
RewriteRule ^(.*)$ http://www.google.com/$1 [R, 301]
Would be something i would try to redirect a request for /members and send them to another domain. (with the same request uri). Further info on mod_rewrite can be found https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
You are using the F flag in your rewrites. The flag is to send the Forbidden status back to client. If you want to redirect the user to another URL, you'll need to pass that URL as the second parameter to RewriteRule directive with the R flag:
RewriteEngine On
RewriteRule ^members\b http://some-other-website.com [R]

Mod_rewrite (htaccess) QSA and removing charactes from appended query string

I've been struggling with some htaccess redirects. I just spent some time reading and searching and couldn't get a solution that works with my scenario.
I'm in the process of making the 301 redirect for an old website (ASP) to a new one (Wordpress). The old pages has parameters query which I need to process but also remove 'http://' string from it to get redirect to work.
Example URL (old) to redirect looks like:
http://www.domain.org/index.asp?documentID=2410&utm_source=IT+Travel+Reminder&utm_medium=Informz%2FnetFORUM&utm_campaign=IT%2FTravel+Reminder%2FMonthly+Monthly+Travel+Reminder&zbrandid=4050&zidType=CH&zid=28841368&zsubscriberId=1036792259&zbdom=http://my.informz.net
redirected to:
http://www.domain.org/permalink-2410/?qs=true&utm_source=IT+Travel+Reminder&utm_medium=Informz%2FnetFORUM&utm_campaign=IT%2FTravel+Reminder%2FMonthly+Monthly+Travel+Reminder&zbrandid=4050&zidType=CH&zid=28841368&zsubscriberId=1036792259&zbdom=my.informz.net
and .htaccess code to redirect it:
RewriteCond %{QUERY_STRING} ^documentid=2410(&.*)$ [NC]
RewriteRule ^index\.asp(.*):(.*)$ http://www.domain.org/permalink/?qs=true%1%2 [L,R=301,QSA]
but somehow is not working as I have expected when
RewriteCond %{QUERY_STRING} ^documentid=2410(&.*)$ [NC]
RewriteRule ^index\.asp$ http://www.domain.org/permalink/?qs=true%1 [L,R=301,QSA]
works fine when I will remove http:// or : from a query string.
Where do I have made mistake?
Thanks!
Try this rule:
RewriteCond %{QUERY_STRING} ^documentid=(\d+)(&.+?)http://(.+)$ [NC]
RewriteRule ^index\.asp$ http://www.domain.org/permalink-%1/?qs=true%2%3 [L,R=302,NC,NE]
Make sure to clear browser cache before testing this.