Redirect a domain already redirected - .htaccess

I have a domain pointing to another like so:
http://domain1.com => http://domain2.com/foo/bar
domain1.com is being redirected at the dns level and I have no control/access.
I need to send traffic from domain1.com/baz to domain2.com/baz but keep getting a 404.
Right now, when you hit domain1.com/baz in the browser, it sends me to domain2.com/foo/bar?q=baz.
I tried adding Redirect 301 /foo/bar?q=baz /baz to my .htaccess but I still end up on /foo/bar?q=baz with a 404.
Is there a way to fix this?

Try this in your .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)q=([^&]+)(&|$)
RewriteRule ^foo/bar$ /%2? [L]

Related

Redirect ALL pages from an old Domain to a new page on a different domain

I am trying to use htaccess to redirect ALL pages from a domain to a specific page on a new domain. Yes, I completely understand we will loose SEO value this way.
I currently have a cpanel redirect that makes this url:
https://www.kiss1047.net/
go to this
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/
but that doesn't get any of the internal pages to redirect. I would also like all internal pages (here is an example):
https://www.kiss1047.net/listen-live
to also go to:
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/
I have tried a few things, but they always carry over the page url, ie above /listen-live/
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/listen-live/
and that results in a 404.
is there some htaccess magic i can employ here?
In your .htaccess file .. Make a single entry that looks like:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) https://mytown-media.com/stations/kiss-104-7-kxnc-fm/ [R=301,L]
This will direct ALL TRAFFIC (.*) to your other website .. Regardless of file name or directory .. And will not append the file/directory to the end of the URL .. IE listen-live
This is a 301 Permanent redirect [R=301,L] .. Which means once followed by Google, will be indexed as such .. Also will cache in users browsers so that the browser remembers the 301 instead of bouncing between websites as well.
This command in .htaccess redirects every page of your old domain to new domain's one specific URL.
RewriteEngine on
RewriteRule ^(.*)$ http://www.newdomain.com/ [R=301,L]
For your case:
RewriteEngine on
RewriteRule ^(.*)$ https://mytown-media.com/stations/kiss-104-7-kxnc-fm/ [R=301,L]
In result:
https://www.kiss1047.net/listen-live
will be redirected to:
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/

.htaccess redirect http to https and redirect main page visitor to another page

I know this has been asked several times, but I have checked a lot of the other answers and still couldn't solve my problem.
Let's assume my website is example.com
I want to redirect all http address to https, and also redirect all example.com visitors to example.com/main
This is what I tried:
RewriteEngine On
RewriteRule ^(.*)$ https://www.example.com/main/$1 [R,L]
It sort of works, but there are some issues.
If I type www.example.com/main or example.com/main it goes to the http site, instead of the https. Why is that?
Also, if I type example.com/works/01 it becomes https://www.example.com/main/works/01. It adds an extra 'main' folder in.
How to fix this problem?
You need to check if the request is comming from http URL scheme and then redirect that request to a secure connection
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/main/$1 [R,L]

DNS 301 redirect issue

I implemented a DNS-level 301 redirect in my Namecheap console, which redirected my old sub-domain assets.websitename.com to websitename.com/assets.
However, when trying to access assets.websitename.com/css/main.css, the client will be redirected to websitename.com/css/main.css/assets. I intended to redirect the user to websitename.com/assets/css/main.css.
So my question is:
How can I properly configure the 301 redirect?
Apache
If using .htaccess or similar in Apache web server:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} assets.websitename.com$ [NC]
RewriteRule ^(.*)$ http://websitename.com/assets/$1 [L,R=301]
Namecheap
Since you're using the Namecheap control panel, try this:
Make sure you have a slash at the end of the IP Address / URL box. So it should look like this:
HOSTNAME IPADDRESS/URL REDIRECTTYPE
assets http://websitename.com/assets/ URL Redirect
notice the slash at the end of http://websitename.com/assets/

How to 301 redirect domain.com to domain.com/

What is the htaccess code to 301 redirect my homepage from www.domain.com to www.domain.com/ and domain.com should also go to www.domain.com/
the non-www to www works fine, all internal redirects work, e.g. www.domain.com/abc/index.php goes to www.domain.com/abc/
However I also want that the homepage itself goes from .com to .com/
Please advise.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
There is no such URL as http://www.domain.com. This is how we write it in the browser (or just www.domain.com) but in the background, the browser connects to www.domain.com and asks for / no matter if it is present in the URL or not.
In other words, http://www.domain.com is, in fact, a shorter way to write http://www.domain.com/.
There is no point in rewriting or redirecting www.domain.com to www.domain.com/. The most you can get if you try to do it is an infinite loop.

Redirect domains and subdomains with .htaccess

Right now *.foo.com and *.bar.com are aliased to the same vhost with LAMP. Right now it, of course, shows the same content with every URL. But I don't actually want this to be what happens, and I want the other domains to actually redirect to foo.com. I'm assuming this is possible with the .htaccess file.
EDIT: I'm hosting this on my own server, so I'd also like the IP address to redirect to foo.com
Use a rewrite condition in your .htaccess to selected everything not for foo.com and redirect it then:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^foo\.com$
RewriteRule ^(.*)$ http://foo.com/$1 [R=301,L]

Resources