Convert links to another domain - .htaccess

I want convert links to another domain
From this : www.old.net/upload/namefolder/namefile.mp3
To : www.new.net/upload/namefolder/namefile.mp3
using .htaccess
How can I do this?

Try this hope it will work fine.
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(?:mp3)$
RewriteCond %{HTTP_HOST} old\.net [nc]
RewriteRule ^(.*)$ http://www.new.net/$1 [R=301,L,QSA]

You can use something like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.old\.net$ [NC]
RewriteRule ^(.*)$ http://www.new.net [R=301,L]

Related

Rewrite an URL but keep the original one

I need to keep the original URL from my subdomain to a specific page :
I tried the following but not working :
RewriteEngine On
RewriteBase /
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^my-subdomain.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/page1/ [R=301,NC,L]
I get : http://domain.com/page1/
I want : http://my-subdomain.domain.com
I think this is what you want:
RewriteEngine On
RewriteBase /
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteCond %{REQUEST_URI} ^/page1 [NC]
RewriteRule ^(.*)$ http://my-subdomain.domain.com/ [R=302,L]
Input
http://domain.com/page1/
Output
http://my-subdomain.domain.com/

htaccess redirect to index

Basically, i'm trying to redirect all the pages like this
http://www.example.com/page-test-1/page-test-1-2/page-test-1-2-3.html
or
http://www.example.com/page-test-1/page-test-1-2.html
or
http://www.example.com/page-test-1/page-test-1-2/
to the home page :
http://www.example.com/
Here's what I've trying :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.exmaple.com/([^.]+)/
RewriteRule ^(.*) http://www.exmaple.com/ [QSA,L,R=301]
And here's the tester I'm using : http://htaccess.madewithlove.be/
Any way I can do this ? Much appreciated.
This rule should work for you:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.exmaple\.com$ [NC]
RewriteRule ^page-test-1/.+ / [L,R=301,NC]
This will catch anything that is not /anypage.html and redirect it. You need to set the RewriteCond on the REQUEST_URI not the HTTP_HOST.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/([^/]*)\.html$
RewriteRule ^(.*) http://www.example.com/ [QSA,L,R=301]

.htaccess rewrite multiple urls without redirects

Ok so i have urls like these demo1.domain.net, demo2.domain.net, demo3.domain.net and all needs to show the contents of a subfolder like this example
demo1.domain.net will show the contents of domain.net/websites/demo1
i need to do it in a way that all the domains like $whatever.domain.net into domain.net/websites/$whatever.domain.net, i already enabled wildcard dns, and i cant seem to fiqure it out, please keep in mind i dont want it to redirect.
Here is what i tried
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.domain\.net.*$
RewriteRule /websites/$1 [L]
I just get the main page of domain.net with this.
Your help will be greatly appreciated.
Close, try this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/websites/
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+\.domain\.net.*)$ [NC]
RewriteRule ^(.*)$ /websites/%2/$1 [L]
Note that this will only work if the all the demo*.domain.net point to the same document root as the main domain (domain.net).
I think, I can manage this! Could you please try these .htaccess directives AGAIN:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.net$
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.net$
RewriteRule ^/?$ http://domain.net/websites/%2 [P]
It will rewrite $whatever.domain.net into domain.net/websites/$whatever.
NOW if it's suppose to be $whatever.domain.net into domain.net/websites/$whatever.domain.net then you could try this one:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.net$
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.net$
RewriteRule ^/?$ http://domain.net/websites/%2.domain.net [P]
Or maybe this code below is what you've wanted in the future:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.net$
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.net$
RewriteRule ^(.*)$ http://domain.net/websites/%2.domain.net/$1 [P]

Very easy htaccess redirect works in the opposite way

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://agilerent.com$ [NC]
RewriteRule ^(.*)$ http://www.agilerent.com/$1 [R=301,L]
this is not working, while this is working:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://agilerent.com$ [NC]
RewriteRule ^(.*)$ http://www.agilerent.com/$1 [R=301,L]
with the negation in front of the condition. I've read a lot of material about .htaccess in last hour, but I can't realize yet what am i doing wrong...
%{HTTP_HOST} contains domain name ONLY, e.g. example.com, www.example.com etc and does NOT include protocol. Therefore:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^agilerent\.com$ [NC]
RewriteRule ^(.*)$ http://www.agilerent.com/$1 [R=301,L]

How to use "NOT" operator on .htaccess redirection?

I'm using .htaccess to redirect users from multiple old domains to a new domain. Something like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} oldsite1.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} oldsite2.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} oldsite3.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]
However, I'd like to know if I could use the NOT operator to redirect any domain except newsite.com, like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond != %{HTTP_HOST} http://newsite.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]
Thank you very much!
Official manual: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
You have placed != in wrong place PLUS %{HTTP_HOST} contains domain name only so no protocol should be there:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !=newsite.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]

Resources