How to redirect to another page using htaccess - apache

I have 2 websites 1 old website and 1 new website. both are on different domains. now the old website had many urls like www.test.com/sadfd/dsf etc etc... all these urls are indexed by google. So when users click these urls I want them to redirect to www.newdomain.com
I had added a redirect using redirect in htaccess but then if the base url is www.domain.com/something/something it will redirect to www.newdomain.com/something/something.. but that url does not exist. I need all visits to redirect to www.newdomain.com
how can I do this?

I'm not the master of the .htaccess file but you can use this snippet, it redirects all pagerequests to the domain without adding the path:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?name-of-domain\.net$ [NC]
RewriteRule ^(.*)$ http://www.new-domain.net/ [R=301,L]

Related

Redirect old site to new address, but one page to new URL

I want to redirect all of my old domain request to my new domain, but except one subpage. One subpage from old site, must be redirect to URL on new site. For example:
siteA.com/* -> siteB.com/#/home/
siteA.com/documents -> siteB.com/#/home/addDocuments
Below is what I am using but it does not work, because old page with documents no redirect to new site with addDocuments, but only to home new page.
Redirect 301 /documents https://siteB.com/#/home/addDocuments
RewriteCond %{REQUEST_URI} !^/dokumenty [NC]
RewriteRule ^(.*)$ http://siteB.com/ [L,R=301]
I must do it in vhost configuration file. Any suggestions?
Problem solved:
(on server siteA)
RewriteEngine on
Redirect 301 /documents https://siteB.com/#/home/addDocuments
RedirectMatch 301 ^/ https://siteB.com

Redirecting some sub pages to another format page using htaccess

I would like to add 301 redirects to specific type subpage on my site which is hosted on LAMP server.
for example,
example.com/witcher-3-100309/player-count - example.com/witcher-3-100309?tab=player-count
example.com/dota-2-100209/player-count - example.com/dota-2-100209?tab=player-count
example.com/pubg-300100/player-count - example.com/pubg-300100?tab=player-count
Is there any way in htaccess to write a general rule for all these type URLs to redirect correctly instead of individual 301 redirect codes in htaccess.
Thanks in advance.
The following should do it
RewriteEngine on
RewriteCond %{THE_REQUEST} /.+\?=([^\s]+) [NC]
RewriteRule ^.+$ %{REQUEST_URI}?tab=%1 [NE,L,R]
This will redirect /foobar/?q=value to /foobar/?tab=value .
When you sure the rule is working ok you can change the R (Temp Redirect flag) to R=301 to make the redirection permanent.

Redirect everything from domain A to domain B homepage, except redirect one specific pdf file

I'm using .htaccess to 301 redirect everything on domain A to the homepage of domain B, but I need it to ignore one specific pdf file URL on domain A, and have that domain A pdf file URL redirect to a URL on domain B.
I'm using this currently to redirect just the pdf file -
Redirect 301 /directory/pdffile.pdf http://domainB.com/wnewpdffile.pdf
But when I try to add the following rule after the pdf rule, it just redirects everything, and ignores my pdf rule.
RewriteEngine On
RewriteRule ^(.*)$ http://domainB.com/ [R=301]
How can I use both rules together? Thanks
Try this:
RewriteEngine On
RewriteRule ^directory/pdffile.pdf$ http://domainB.com/wnewpdffile.pdf [R=301,NC,L]
RewriteRule ^(.*)$ http://domainB.com/ [R=301]
Where "L" flag tells Apache not to process any more rules if this one is used.

How to use htaccess to redirect only certain URL's to a different domain

I have two ecommerce websites abc.com and pqr.com .In a couple of months time I plan to close down abc.com and have started listing all products on pqr.com . How can I redirect only a certain urls on the old website to the new one keeping other URL's as is.
e.g. abc.com/example-product should redirect to pqr.com/example-product
abc.com/example-product-2 should not redirect.
You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?abc\.com$ [NC]
RewriteRule ^example-product/?$ http://pqr.com/$0 [L,NC,R=301]

how to redirect a subdomain with child directory to new url using htaccess

I have a old website which used subdomains for each pages, i'm moving to new site and want to redirect the most of the links to the new pages.
I searched a lot but didn't find anything.
For instance i will be redirect this url to this:
blog.test.com/12/09/test-2 to test.com/blog/test-2
How it possible?
I tried this
or just simply redirect all the blog.test.com with whatever query they have to the new page?
blog.test.com/whatever?whatever to test.com/blog
You can use this code in your DOCUMENT_ROOT/.htaccess file of test subdomain:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(blog)\.(test\.com)$ [NC]
RewriteRule ^\d{2}/\d{2}/(.+)$ http://%1/blog/$1 [L,R=302]