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

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.

Related

Htaccess Redirect URL with two forward slashes (not double) won't work

I want to redirect from one domain to a new domain. At the same time, the URL structure has changed.
Old: https://www.olddomain.com/parentpage/oldtitle/
New: https://www.newdomain.com/newtitle
This is wordpress, and I placed this code above the Wordpress stuff, as well as tested it here: https://htaccess.madewithlove.be/
I tried this, which doesn't work:
Redirect 301 /parentpage/title https://www.newdomain.com/newtitle
Also, when testing it at https://htaccess.madewithlove.be/, I do have this redirect:
Redirect 301 /parentpage https://www.newdomain.com/parentpage
The tester would skip my preferred redirect above, and use this one, leaving me with this, which does not exist:
https://www.newdomain.com/parentpage/oldtitle
Even when I place the preferred redirect above this one. I need both, unfortunately.
Have also tried the following RewriteRules (not all at the same time)
ReWriteRule https://www.olddomain.com/parentpage/oldtitle/ https://www.newdomain.com/newtitle
ReWriteRule /parentpage/oldtitle/ https://www.newdomain.com/newtitle
ReWriteRule "https://www.olddomain.com/parentpage/oldtitle/" "https://www.newdomain.com/newtitle"
I think it has something to do with that second forward slash separating the parentpage name and page title, but I can't figure out how to fix it.
In RewriteRule it wouldn't match http or https in it, you may try following.
please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)olddomain\.com [NC]
RewriteCond %{REQUEST_URI} ^/parentage/oldtitle/?$ [NC]
RewriteRule ^(.*)$ https://www.newdomain.com/newtitle [R=301,L]

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

redirect url already rewritten in htaccess

I have written htaccess
my actual url is
https://www.example.com/index.php?route=information/partnership_form
rewritten as
https://www.example.com/for-brands/partner-with-us
which works fine as I have written rule as
`RewriteRule ^for-brands/partner-with-us$ https://www.example.com/index.php?route=information/partnership_form [NC,L]`
but I want redirection if some user visits direclty https://www.example.com/index.php?route=information/partnership_form
to
https://www.example.com/for-brands/partner-with-us
below is my code redirect but I've tried many ways form other links of stackoverflow still I cant find any solution
rewriterule ^index\.php?route=information\/partnership_form(.*)$ /for-sports-brands/partner-with-us$1 [r=301,nc]
You can not manipulate querystring like that. You need to use a RewriteCond
RewriteEngine on
RewriteCond %{QUERY_STRING} ^route=information/partnership_form [NC]
RewriteRule ^index\.php$ /for-brands/partner-with-us? [NC,L,R]
The ? at the end of the rewrite target is important as it discards the old querystrings.

Proper htaccess rewrite

I'm trying to force an old URL to go to the new url and my code seems to have no effect
RewriteCond %{QUERY_STRING} ^index2\.php?page=shop\.product_details&\.tabs\.tpl&product=310&category=71&Itemid=2$
RewriteRule .* /hunting/back-packs/multi-packs/black-2.5-pack.html [R=301,L]
my old url is
www.mywebsite.com/index2.php?page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2
and my new one is
www.mywebsite.com/hunting/back-packs/multi-packs/black-2.5-pack.html
My code does not break anything but does not work either
Thanks in advance
As I understand you want redirect (301 Permanent Redirect) so the URL will change in browser. This will work for this URL ONLY /index2.php?page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2:
RewriteCond %{QUERY_STRING} =page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2 [NC]
RewriteRule ^index2\.php$ http://www.mywebsite.com/hunting/back-packs/multi-packs/black-2.5-pack.html [R=301,L]
If you want internal redirect (rewrite), then use these lines:
RewriteCond %{QUERY_STRING} =page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2 [NC]
RewriteRule ^index2\.php$ /hunting/back-packs/multi-packs/black-2.5-pack.html [L]
PLEASE NOTE:
You need to put these lines in a proper order (order of rules matters) otherwise (if you put it at the end) some another rule will rewrite it to a different URL.
This needs to be placed in .htaccess file in website root folder. For any other location you may need to modify it a bit.