Redirect URL with wildcard variables - .htaccess

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]

Related

htaccess redirect including string

I know how to redirect normal links without strings, but including the string for email addresses has me stuck. Can someone please assist with 301 redirecting a URL to another domain including the URL string? I need to redirect the following:
www.domain.com/page.php?e=tom-jones#mail.com
to
www.domain1.com/?e=tom-jones#mail.com
I just need to keep the email address in the string when it's redirected to the new URL
Thanks in advance.
In the htaccess file in your www.domain.com document root, you can just add:
RedirectMatch 301 ^/page\.php$ http://www.domaon1.com/
The query string will automatically get appended. Or you can use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^page\.php$ http://www.domaon1.com/ [L,R=301]

Redirect only one URL with parameter

I need to redirect URL, for example:
www.mydomain.com/category/sub-category/product?page=shop.ask&flypage=flypage.tpl&product_id=181&category_id=34
To:
www.mydomain.com/category/sub-category/good-product.html
I have a multiple URLs with parameters that need to be redirected to only one or couple of URLs, can you help me, I used Google for hours.
I was try this code at .htaccess:
redirect 301 /category/sub-category/product?page=shop.ask&flypage=flypage.tpl&product_id=181&category_id=34 www.mydomain.com/category/sub-category/good-product.html
But it doesn't work.
You can't match against the query string in a Redirect directive. You'll need to use mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=shop.ask&flypage=flypage.tpl&product_id=181&category_id=34$
RewriteRule ^category/sub-category/product$ /category/sub-category/good-product.html? [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