How to add redirects to pages with a query string? - apache

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]

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]`

301 Rewrite Rule Not working

We want to redirect Below mentioned URL.
From URL:
www.example.com/itemLevelFilterPage.action?keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0
To URL : www.example.com/4G
We wrote 301 rules in apache configuration as below.
RewriteEngine on
RewriteRule ^/itemLevelFilterPage.action?keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0 /4G [L,R=301]
But redirection is not working as expected. Any Suggestion will be highly appreciated.
mod_rewrite will not look at query strings as part of its matching, unless you specifically ask it to:
RewriteCond %{REQUEST_URI} ^/itemLevelFilterPage\.action$
RewriteCond %{QUERY_STRING} ^keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0$
RewriteRule (.*) http://www.example.com/4G? [R=301,L]
The first condition makes sure we're looking at the right page, the second checks the query string, and the rule will rewrite the URL to the desired one.
The trailing question mark on the rule ensures the old query string is removed. If you're on Apache 2.4 the query string discard flag is available:
RewriteRule (.*) http://www.example.com/4G [R=301,L,QSD]
More examples here.

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.

How to redirect direct hits

I'm sure this has been asked before, but I have no idea what it would be called.
I have done a quick Google and search here on SO.
Perhaps it would be better asked, how do I redirect http://site.com/shop to a specified URL like http://othersite.com/page without it affecting http://site.com/shop/?q=test or any other $_GET's.
RewriteRule ^shop http://othersite.com/page [R=301,L]
Works fine apart from it also affects links like http://site.com/shop/?q=test which I want to work.
Cheers.
You can use mod_rewrite to match against the query string (or to see if there is no query string):
RewriteEngine On
# blank query string
RewriteCond %{QUERY_STRING} ^$
# check host
RewriteCond %{HTTP_HOST} site\.com$ [NC]
RewriteRule ^shop/?$ http://othersite.com/page [R=301,L]

apache rewrite rules

i'm trying to get the following url to be rewritten
http://blog.mywebsite.com:8080/wp-admin/users.php?paged=2
as
http://mywebsite.com/blog/wp-admin/users.php?paged=2
i have tried a variety of rewrite conditions and rules
RewriteCond %{QUERY_STRING} ^(.*)$ [NC]
RewriteRule blog.mywebiste.com(.*)$ mywebsite.com/blog/$1
i seem to loose the query string when id do this
RewriteRule ^/wp-admin(.*) http://mywebsite.com/blog/wp-admin$1
this doesn't seem to catch it at all
also i have the rewrite log on. When does this get written to?
It should work without leading forward-slash:
RewriteRule ^wp-admin/(.*) http://mywebsite.com/blog/wp-admin/$1