redirectmatch without parameters? - .htaccess

The url
www.ex.com/product_info.php?products_id=23&osCsid=1ed12
should be redirect to www.ex.com/pagex.htm
I've tried the .htaccess with
"RedirectMatch 301 ."star"product_info.php"star" https://ex.com/pagex.htm"
(where "star" = *)
but then the parameters are being forwarded to, so
https://ex.com/pagex.htm?products_id=23&osCsid=1ed12
How to get rid of "?" and everything on parameter stuff?

RedirectMatch directive doesn't remove query string. Use mod_rewrite based rules as this one:
RewriteEngine On
RewriteRule ^/?product_info\.php$ https://ex.com/pagex.htm? [L,NC,R=301]
Trailing ? strips off any existing query string in target URL.

Related

How to prevent append parameter in 301 redirect

I am using this redirect command in .htaacess file.
Redirect 301 /old-path.html /new-path.html
It shows me /new-path.html?page=old-path
Probability, appended parameter is result of some RewriteRule and RewiteCond in my htaccess.
I don't like to edit my original htaccess codes.
I found below command is very close to the result.
RedirectMatch 301 /old-path.html /new-path.html?
The result is /new-path.html? which has only an extra ?
Don't use RedirectMatch because it doesn't have a way to discard old/previous query string.
It is better to use a RewriteRule like this:
RewriteRule ^old-path\.html$ /new-path.html? [L,R=301,NC]
Trailing ? in target will discard any query string.
On Apache 2.4+ you can also use:
RewriteRule ^old-path\.html$ /new-path.html [L,R=301,NC,QSD]
As a thumb rule place all R=... rules on top of your .htaccess before other rules. This will make these rules work before other internal rewrite rules that may modify request URI or query string.

RedirectMatch for multiple urls with queries

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.

.htaccess redirect with questionmark

I want to redirect certain urls matching a pattern to the site root.
I'm doing:
RedirectMatch 301 ^/ABCDE.*$ /
But whenever there is a ? in the url the resulting link is whatever is after the url. How can I redirect also urls including questionmarks?
To strip off existing query string you need to use mod_rewrite rules. Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^ABCDE.*$ /? [L,R=301,NC]

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]

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