uri redirect with domain change - php

I'm not the best at this but I have to do a redirect
domain1.tld/subfolder/seo/based/uri
and i need to redirect to
subd.domain2.tld/seo/based/uri
is that possible?
EDIT:
Forgot to mention, these all dynamic requests. Like:
domain1.tld/forums/forum/2-network-announcements/topic-name-with-id

This can be done in .htaccess or PHP, but I find it's easier in PHP.
To do it this way, the PHP file for the first URL should contain:
<?php
header("Location: http://subd.domain2.tld/seo/based/uri");
?>
More info here: http://php.net/manual/en/function.header.php

Redirecting from .htaccess is immediate. Meaning, if you redirect from .htaccess, no code will be run at domain1.tld/subfolder/seo/based/uri. If that's what you want, then add these lines to your .htaccess:
RewriteEngine On
RewriteRule ^domain1.tld/subfolder/(.*)$ subd.domain2.tld/$1
If you do want to run some code on domain1.tld/subfolder/seo/based/uri, then go read #tomtheman5's answer, because that will be closer to what you want.

In the htaccess file or the vhost for domain1.tld, add:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.tld$ [NC]
RewriteRule ^/?subfolder/(.*) http://subd.domain2.tld/$1 [L,R=301]
You could also use mod_alias if the 2 hosts have different document roots:
Redirect 301 /subfolder http://subd.domain2.tld

Related

Permanent redirect htaccess to a new domaine keeping a part of the URL as it is

I never wrote any htaccess codnitions and the one by default dont give me enough to work with to get what I need,
I have a URL like : http://domaineA.com/target:/to/be/removed/keep/this.ext
And I want to redirect all URLs like this one to :
http://domaineB.com/keep/this.ext
I tried this, but didn't work:
//301 Redirect Entire Directory
RedirectMatch 301 http://domaineA.com/target:/to/be/removed(.*) http://domaineB.com/$1
Update:
I forgot to mention that the /keep/this.ext is dynamic, it represents all files of a my directory.
Thank you
You can use this rule as your very first rule in site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domaineA\.com$ [NC]
RewriteRule ^/?target:/to/be/removed(/.*)?$ http://domaineB.com$1 [L,NE,R=301,NC]
you can put this in your .htacces or your virtualhost as below
Redirect permanent http://domaineA.com/target:/to/be/removed/keep/this.ext http://domaineB.com/keep/this.ext

.htaccess 301 redirect conflicting with rewriterule

I am trying to redirect the URL but its conflicting with the rewrite-rule, I am using this redirection Rule
Redirect 301 /antique-vintage-rugs/170-antique-oriental-rugs-carpets https://example.com/antique-vintage-rugs/170-antique-rugs
it took me to this link
https://example.com/antique-vintage-rugs/170-antique-rugs?action=clear&template=170-antique-oriental-rugs-carpets instead of this
https://example.com/antique-vintage-rugs/170-antique-rugs
My Rewrite URL is this
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^antique-vintage-rugs/(.+) results.php?action=clear&template=$1 [NC]
Is there any way I can get the desired output redirect link?
You need to only be using mod_rewrite here instead of mixing it with mod_alias (where the Redirect directive belongs). When you mix the two, both end up processing the same request.
So instead of Redirect, use:
RewriteRule ^antique-vintage-rugs/170-antique-oriental-rugs-carpets https://example.com/antique-vintage-rugs/170-antique-rugs [L,R=301]
but make sure it's before your other rules.
I had a somewhat similar problem. I was trying to add redirects from cpanel while I already had some rewrite rules written in my .htaccess file. The error I got was "No maching tag for "
What I ultimately did was that kept a copy of my existing rules and cleaned the .htaccess. Then went and added all the redirects that I needed from cpanel, and then at the end put back my own rewrite rules in the end of the file. That worked for me

Need to transfer from one domain to another domain

I want to redirect permanent from one domain to another, such as:
http://www.example.de/page.html?cid=00340119050014953926&pc=70000
to this:
http://www.secondexample.com/page.html?cid=00340119050014953926&pc=70000
So it's a different domain with the same parameters.
How this can be done?
This can also be done with an htaccess redirect.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*) http://www.secondexample.com%{REQUEST_URI} [R=302,NC]
Change your index file in your old domain to this:
<html>
<?php
header('Location: http://www.secondexample.com/page.html?cid=00340119050014953926&pc=70000');
exit;
?>
You can simply use a header redirect.
If you need to keep the old get data you can include simple parsing in yout old do,ain of the get data and use it in the redirect.
See PHP header for more info.
If the goal is to always redirect the page.html page to the new domain regardless of what's in the query string, you can do this in htaccess with something similar to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^page.html(.*)$ http://www.secondexample.de/page.html$1 [R=301,L]
</IfModule>
If these domains are on a shared server and actually point to the same files as one another, you'll need to add a condition above the RewriteRule in the above:
RewriteCond %{HTTP_HOST} ^example.de$
can be done using .htaccess file
RewriteCond %QUERY_STRING ^(.+)$
RewriteRule page\.html http://www.secondexample.com/?%1 [R=301]

Unique url htaccess or other redirect needed

I need to somehow make one unique url redirect.
For instance:
http://www.mydomain.com/shop needs to redirect back to http://www.mydomain.com
BUT
http://www.mydomain.com/shop/tshirts etc still needs to function, I cant manage to get this to work. No matter what I attempt anything containing /shop/xxx redirects which I dont want it to.
Thanks in advance!
Try this. Write this in .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_URI} /shop
Rewriterule ^$ http://mydomain.com/test/ [L,R=301]
You need to enable MOD_REWRITE.
Place a .htaccess file in the root folder of your server and have it contain:
RewriteEngine On
RewriteRule ^(.*)shop/$ http://mydomain.com/ [R,L]
Hope it works for you!
Your redirection seems simple enough, so you can use redirect directive for this.
In your .htaccess file, add this redirect.
Redirect 301 /shop http://www.mydomain.com/
301: permanent
302: temp
303: seeother
410: gone
mod_alias needs to be enabled in config.

How to redirect in url by using .htaccess in php?

How to redirect in url by using htaccess in php?
http://www.example.com/randomstring/2
i would like to go to id page 20 thank
You should use mod_rewrite for example in this way:
RewriteEngine On
RewriteRule ^(randomstring)/([0-9]+)$ /randomstring/$1/ [QSA,R]
RewriteRule ^(randomstring)/([0-9]+)/$ index.php?page=$1&id=$2 [QSA,NC,L]
If you want to simply redirect from a domain to another just write:
RewriteRule ^(randomstring)/([0-9]+)$ http://www.example.com/$1/$2/ [QSA,NC,L,R=301]
use mod_rewrite, it's a module for apache. You can see the documentation here. .htaccess files control the webserver, not php, so you have to look there.
Copy that into your .htaccess file in the directory you need it:
RewriteEngine On
RewriteRule ([A-Za-z0-9]+)/([0-9]+)$ index.php?id=$2
www.domain.tld/asdf/2 --> index.php?id=2
www.domain.tld/asdfa923als/52 --> index.php?id=52
;-)

Categories