301 redirection in htaccess not working - apache

I have and issue when I'm trying to redirect my old domain to my new domain. Things look like that right now:
newdomain.pl <- is connected to blogger account
olddomain.pl <- is connected to server with .htaccess file (before it was connected to blogger account)
I would like it to work like that : olddomain.pl/subdomain -> redirect to newdomain.pl/subdomain. But whatever I try to put into my .htaccess file it's redirecting my old site to main page of newdomain (newdomain.pl). I've tried codess like that:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^newdomain.pl[nc]
RewriteRule ^(.*)$ http://www.newdomain.pl/$1 [r=301,nc]
but it's also redirecting to main page of new domain. Only when I put:
RedirectRule / http://www.newdomain.pl
It's redirecting to http://www.newdomain.plsubdomain <- but there is slash "/" missing between the newdomain name and subdomain. Unfortunately when I write this:
RedirectRule / http://www.newdomain.pl/
it's redirecting to main domain page http://www.newdomain.pl, so it's not working. I don't know what can be wrong, I'm fighting with that three days already. Does anyone have an idea where can be the problem? Maybe there is something wrong with hosting that I bought? Thank you in advance for any response
Regards, Pawel

Try next:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R]
For only redirecting queries without direct redirecting to domain remove R flag.

Try this in olddomain/.htaccess :
RedirectMatch ^/(.*)$ http://newdomain.com/$1

Ok I found a solution. The problem was not including "www" in RewriteRule. When I changed it, it worked. I'm putting the code that worked for me below:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.olddomain.pl$
RewriteRule ^(.*)$ http://www.newdomain.pl/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^olddomain.pl$
RewriteRule ^(.*)$ http://www.newdomain.pl/$1 [L,R=301]
Thank you everyone!

Related

.htaccess redirect to subdomain url with hash

I'm trying to redirect a site with the following url structure:
https://example.com/2021/about
to
https://2021.example.com/#about
https://example.com/2021/visit
to
https://2021.example.com/#visit
how can i do this?
i tried adding this to the /about directory in the original domain:
RewriteEngine on
RewriteBase /
RewriteRule (.*) https://2021.example.com/#about [R=301,NE, L]
but what i got after the redirect was
https://2021.example.com/#about2021/about
which is not right. any help is appreciated
[EDIT] i only want to apply this to some folders, like /2021/about and 2021/visit
You may use this redirect rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com) [NC]
RewriteRule ^(20\d{2})/(.+?)/?$ https://$1.%1/#$2 [R=301,L,NE]
Make sure this is your topmost rule in .htaccess and you clear your browser cache before testing this new rule.
Could you please try following, written based on your shown samples. Please make sure to clear your browser cache before testing your URLs. Following will only be applied to 2021/about OR 2021/visit uris.
RewriteEngine ON
RewriteCond %{HTTP_HOST} (example\.com) [NC]
RewriteRule ^(2021)/(about|visit)/?$ http://$1.%1/#$2 [R=301,NC,NE,L]

.htaccess redirect to another domain except admin page

I am asking because I can not for the live of me figure out what is wrong and so far none of the StackOverflow answers worked.
I have to redirect a domain to another subdomain, except the admin. For example:
sub1.domain.com/testsite/ shoud redirect to "sub2.domain.com/testsite/",
but sub1.domain.com/admin/ or "sub1.domain.com/de/admin/" should stay right where it is.
As a first step I tried to only check for the "admin", so everything would be redirected except "sub1.domain.com/admin/":
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com
RewriteCond %{REQUEST_URI} !^\/admin
RewriteRule ^/?(.*)$ http\:\/\/sub2\.domain\.com%{REQUEST_URI} [R=301,L]
This one looked most promising, but it is not working. The second condition is not working and the admin page still gets redirected.
If anyone can help I would appreciate it.
EDIT:
I should have said that its a multi-domain site, which means we have a .htaccess file for all sites and that is the reason I specifically check for the domain.
I'm just posting this, but I can't test it!
But I guess this redirects EVERYTHING except that one domain.
RewriteCond %{HTTP_HOST} !^sub1.domain.com/admin/ [NC]
RewriteRule ^/(.*)$http\:\/\/sub2\.domain\.com%{REQUEST_URI} [R=301,L,NC]
I hope it works!
So,
I just found the reason (besides my stupidity). The site I should redirect was a Drupal Site. Thats why all links end up at the same location:
sub1.domain.com/index.php
The reason why my above Rewrite Condition was not working is, that sub1.domain.com/admin is being redirected to sub1.domain.com/index.php, which consequently ends up at: "sub2.domain.com/index.php". The correct rewrite rule looks like that:
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com
RewriteCond %{REQUEST_URI} !^/(admin|index\.php|de\/admin|it\/admin|user|de\/user|it\/user)
RewriteRule (.*) http://sub2.domain.com%{REQUEST_URI} [R=301,L]
This redirects everything except:
sub1.domain.com/admin
sub1.domain.com/de/admin
sub1.domain.com/it/admin
sub1.domain.com/user
sub1.domain.com/de/user
sub1.domain.com/it/user
and of course
sub1.domain.com/index.php
Since the last one also should not be redirected if the user types it in directly, it is not a perfect solution, but I can live with it.
RewriteCond is use to check condition weather to execute .htacess or not
For your case the solution may be as below:-
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(admin)$ http://sub1.domain.com/$1 [R=301,L]
RewriteRule ^(.*)$ http://sub2.domain.com/$1 [R=301,L]

htaccess RewriteRule for folder

I have a bunch of rewrite rules I would like to implement. I would like to redirect anything that has /blog/tag/... to my root url.
For example, all of these:
blog/tag/button-sets/
blog/tag/icons/
blog/tag/order-now/
blog/tag/body-attributes/
Would simply route to: www.url.com
I can do it on a case-by-case basis like below, but would like to redirect a bunch with 1 rule. Any help would be greatly appreciated
RewriteCond %{HTTP_HOST} ^url\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.url\.com$
RewriteRule ^blog\/tag\/button\-sets\/?(.*)$ "http\:\/\/www\.url\.com\/$1" [R=301,L]
Why dont you just remove the button-sets part of your rule like so
RewriteRule ^blog\/tag\/?(.*)$ "http\:\/\/www\.url\.com\/" [R=301,L]
Havent tested it but should be OK?
In stead make it such that it captures everything after the second / after blog/tag/(ANYTHING)/(CAPTURE) which will redirect to www.url.com/CAPTURE
RewriteRule ^blog\/tag\/.*\/?(.*)$ "http\:\/\/www\.url\.com\/$1" [R=301,L]

Redirect %20%E2%80%8E

I have this customer that send an email with http://url/%20%E2%80%8E and everyone is getting a 404 not found. So he asked me for a redirection and I though sure easy. But it looks like is not working... please help!!
I tried this:
redirect 301 /\%20\%E2\%80\%8E http://url/promo-content/
and
redirect 301 /%20%E2%80%8E http://url/promo-content/
and is not working..
I also tried this
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9_-]+)/\%20\%E2\%80\%8E$
RewriteRule ^(.*) http://url/promo-content/%1/ [R,L]
But still not working... any sugestion???
The URI gets decoded first before mod_rewrite rules are applied. Try:
RewriteEngine On
RewriteRule ^\ ‎ /promo-content/ [L,R=301]

Mod Rewrite, Unexpected Results

We are trying to redirect everything from one domain to another with the following
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example2.com%{REQUEST_URI} [R=301,L]
When we visit http://www.example.com/v2sc
We are being redirected to http://www.example2.comv2sc
We would like to be redirected to http://www.example2.com/v2sc considering www.example2.comv2sc is not a valid hostname
Any ideas on how we can accomplish this?
Thank you!
It seems like you're using a .htaccess file for this. In that context the leading slash is not present in %{REQUEST_URI} so it's up to you to put it back in.
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example2.com/%{REQUEST_URI} [R=301]
Please also note that solutions like this should be used only if you cannot edit the main server configuration file. Doing so would allow you to use a cleaner combination of vhosts and Redirect directives that would run much more quickly.