RedirectMatch for multiple urls with queries - .htaccess

I have a bunch of urls like:
url.com/internet-class-detail?schedid=521
url.com/internet-class-detail?schedid=523
url.com/internet-class-detail?schedid=525
url.com/internet-class-detail?schedid=545
Which I'm trying to redirect to:
url.com/internet-training
I've tried this rule in htaccess, but the query strings keep getting printed at end of redirect:
RedirectMatch 301 ^/internet-class-detail.*$ http://url.com/internet-training/
For example, going to:
http://www.url.com/internet-class-details/?schedid=521
redirect you to:
http://url.com/internet-training/?schedid=521
but I just want it to redirect to:
http://url.com/internet-training/

You can not match against query strings in a RedirectMatch directive, to match against query strings you need to use Mod_rewrite.
Try the following in htaccess :
RewriteEngine on
RewriteCond %{THE_REQUEST} /internet-class-detail\?schedid=([0-9]+) [NC]
RewriteRule ^ http://domain.com/internet-training/? [L,R]
Empty question at the end of the target url is important as it discards the orignal query strings.

Related

htaccess redirect adds query and original URL to redirected page

I've tried using each of the following
Redirect 301 /example http://website.com/new-page
RedirectMatch 301 /example(.*) /new-page/$1
RewriteRule ^example/(.*)$ http://website.com/new-page$1 [R=301,L]
In each case the final URL resolves to "website.com//new-page//?q=/example/"
Note that it is adding /?q=/example/ to the final URL.
That might be due to other rules in your .htaccess. Use this rule to capture original string from THE_REQUEST variable. And add ? in the end to strip off query string.
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+example(\S*)\s [NC[
RewriteRule ^ http://website.com/new-page/%1? [R=301,L,NE]

Redirect URL with wildcard variables

I am trying to use the htaccess file to redirect a url with random url variables for example:
somesite/news/training-viewdoc.htm?file=somethingrandom
to
somesite-two/news/training-viewdoc.htm?file=that variable
Any clues as to how to do this. Right now I have....
RedirectMatch 301 /news/training-viewdoc.htm?file=(.*) somesite-two/news/training-viewdoc.htm?file=$1
You can't match against the query string in a RedirectMatch, however, it should automatically be appended so you don't need to worry about it:
Redirect 301 /news/training-viewdoc.htm somesite-two/news/training-viewdoc.htm
If you need to only redirect when there's a file in the query string, then you have to use mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^file=.
RewriteRule ^news/training-viewdoc.htm$ somesite-two/news/training-viewdoc.htm [L,R=301]

301 redirects: match querystring, don't append it to new redirect

I've set up a number of 301 redirects in an .htaccess file, but I'm having problems with a query string that makes the redirect not match.
Example:
Redirect 301 /about/history/?lang=fr http://www.newdomain.com/fr/history
Redirect 301 /about/history/ http://www.newdomain.com/nl/history
So olddomain.com/about/history/?lang=fr now matches the second rule and redirects to http://www.newdomain.com/nl/history?lang=fr.
I want it to take the ?lang=fr literally and not append the querystring to the new redirect.
How do I do that?
Redirect takes an URL-path, which doesn't include the query string. So, the first Redirect never matches.
To achieve what you want, you can try some sort of content negotiation or use mod_rewrite
RewriteEngine on
RewriteCond %{QUERY_STRING} lang=fr
RewriteRule /about/history/ http://www.newdomain.com/fr/history? [R,L]
RewriteRule /about/history/ http://www.newdomain.com/nl/history [R,L]
When everything works as you expect, you can change R to R=301.

301 Redirect not working as expected

I have a strange 301 Redirect problem.
I'm using the following rule
Redirect 301 /catalog/index.php?target=news /news
Oddly, when I visit /catalog/index.php?target=news
I'm redirected to : /catalog/?target=news
The query string isn't part of the URI that the Redirect pattern is matched against. It's removed so you can't attempt to match against it in your statement. You need to use mod_rewrite and a condition that matches against the %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^target=news$
RewriteRule ^/?catalog/(index\.php)?$ /news? [L,R=301]
Those rules should go in the htaccess file in your document root.

htacess 301 redirect with query string from page that does not exist

My old search file used to be named namesearch.php. It's now named search.php. The query string parameters remain the same. I would like to redirect any namesearch request to search.php. Here is what I have below, which is not working.
RewriteRule ^/namesearch.php%1 search.php$1 [R=301,L]
Query strings are automatically appended when the target URI doesn't try to construct its own (i.e. it doesn't have a ? in it). You can use either mod_rewrite or mod_alias:
mod_alias:
Redirect 301 /namesearch.php /search.php
mod_rewrite:
RewriteEngine On
RewriteRule ^/?namesearch.php /search.php [L,R=301]

Resources