Redirect 301 except when a string is present in url with htaccess - apache

I would like to redirect a subdirectory to a subdomain, but i have to maintenance the endpoints for sitemaps.
What I need is:
Redirect 301 /ru/ https://ru.mydomain.com
#Exclude *sitemap*
This kind of urls can't be redirected:
https://www.mydomain/ru/sitemap.xml
https://www.mydomain/ru/sitemap.1.xml
https://www.mydomain/ru/sitemap.3.xml
https://www.mydomain/ru/sitemap_ru.xml
All have sitemap string and i need no be redirected when sitemap word is in url.
Hope can help me, as i take many time trying to fix it without luck.
I need like:
{if *sitemap* is not present}
Redirect 301 /ru/ https://ru.mydomain.com
{/if}
Thanks!

This one should work for you. If the URL on your website (www.mydomain) starts with /ru but not with /ru/sitemap... we redirect (301) to https://ru.mydomain.com/ but without the /ru in the path. e.g. https://www.mydomain/ru/news goes to https://ru.mydomain.com/news
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mydomain$ [NC]
RewriteCond %{REQUEST_URI} ^/?ru(/.*)?$ [NC]
RewriteCond %{REQUEST_URI} !^/?ru/sitemap.*$ [NC]
RewriteRule ^/?ru(/(.*))?$ https://ru.mydomain.com/$2 [R=301,L]
The NC Flag means that this is case insensitive.
You need mod_rewrite installed end enabled!

Related

301 Issues with 2 URLs on same webspace

I have 2 domains, the old domain is mjvandco.co.uk and he wants this redirecting to mjvlaw.co.uk. I have both pointing to the same webspace but when I test the URLs using https://httpstatus.io/ I get different results.
I have the following in my htaccess along with other stuff, but this is the redirect content:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mjvlaw\.co\.uk
RewriteRule (.*) https://www.mjvlaw.co.uk/$1 [R=301,L]
# Remove .html (excluding blog)
RewriteCond %{REQUEST_URI} !^/blog(.*)$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
The only URL that now is not right is this one: http://www.mjvlaw.co.uk/. I used this site
https://mjvlaw.co.uk - goes to https://www.mjvlaw.co.uk
http://mjvlaw.co.uk - goes to https://www.mjvlaw.co.uk
http://www.mjvlaw.co.uk - not work as does not go to https
https://www.mjvlaw.co.uk - fine
However, when I do the same for the old domain it all works as it should and every one below goes too https://www.mjvlaw.co.uk.
https://www.mjvandco.co.uk
https://mjvandco.co.uk
http://mjvandco.co.uk
http://www.mjvandco.co.uk
Am I doing something stupid here? Should I create another webspace and have one folder for the old domain and what for the current one and each having it's own htaccess file?
Thanks. I have done another ticket a month or so back but I am not sure how to change the questions, so I apologise for the similar ticket.
You rule only redirects non-www http URLs to SSL version of your site. To redirect both non-www and www http versions , replace your first rewrite block with the following
RewriteCond ℅{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.mjvlaw\.co\.uk
RewriteRule (.*) https://www.mjvlaw.co.uk/$1 [R=301,L]
Make sure to clear your browser cache before you test this.

Error when redirecting a domain

The thing is I have one domain in different languages, and I´m not able to do some redirects like this:
I have Spanish http://www.domain.es/inicio
And English http://www.domain.en/inicio
Now, I need to move my page http://www.domain.en/inicio to http://www.domain.en/home
Both domains are using the same .htaccess and I´m not able to do a simple:
Redirect 301 /inicio http://www.domain.en/home
Cause that will redirect the spanish /inicio to /home too.
Also tested
RewriteCond %{HTTP_HOST} ^www.domain.en/inicio/$ [NC]
RewriteRule ^(.*)$ http://www.domain.en/home/ [R=301,L]
But thats also failing.
Did you try something like this?
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /inicio/
RewriteRule ^inicio/(.*) /home/$1 [L,R=301]
You should redirect the content of the folder /inicio to folder /home if I got it right. There is no need to redirect everything since the domain is the same
The request should be GET since the users are accessing your website pages

htaccess redirect on a domain that already redirects

We have 3 domains in play here. Domain1.com is the main site, domain2.com is the smaller second site, and domain3.com is a vanity URL that redirects from domain1 to a Spanish version of domain2.
What we need to have happen is for domain3.com/subpage to redirect to domain2.com/subpage, however the original redirect of the vanity URL is overriding any attempts we make.
Is this kind of redirect possible?
Here is the original redirect in domain1.com's htaccess file:
RewriteCond %{HTTP:Host} ^www\.domain3\.com$
RewriteRule (.*) http\://www\.domain2\.com/spanish-version [I,R]
These are the attempts at making the needed redirect happen (also in domain1.com's htaccess file):
RewriteCond %{HTTP:Host} ^www\.domain3\.com/subpage$
RewriteRule (.*) http\://www\.domain2\.com/spanish-version/subpage [I,R]
RewriteRule ^domain3\.com/subpage?$ /spanish-version/subpage [NC,R=302,U]
Rules for your domain3 should look like this :
RewriteCond %{HTTP:HOST} ^(www\.)?domain3\.com$ [NC]
RewriteRule ^/?(subpage_name)/?$ http://www.domain2.com/$1 [NC,R,L]
This will redirect
www.domain3.com/subpage
to
www.domain2.com/subpage
(www.)? in HTTP:HOST variable is to make the "www" part optional in url, so in this case
urls with or without "www" would also work.

Htaccess 301 only part of the redirect works

Hi Im moving a site from one domain to another, and I have created the following .htaccess file, but its not working.
*#Options +FollowSymlinks
RewriteEngine On
redirect 301 http://www.el-netshop.dk/pi/Dækkape_UG150_12_lysegrå_5302_.aspx http://www.el-netsalg.dk/pi/Dækkape_UG150_12_lysegrå_5271_.aspx
RewriteCond %{HTTP_HOST} ^el-netshop.dk$ [OR]
RewriteCond %{HTTP_HOST} ^www.el-netshop.dk$
RewriteRule (.)$ http://www.el-netsalg.dk/$1 [R=301,L]
I would like it to work like this.
Have a list of urls where the url is diffent, with more then just the domain. Ex. in the above the from link contains 5302 but to link is 5271.
Then with the rest, I want it to make a normal redirect.
The above code just do (.*)$ http://www.el-netsalg.dk/$1 and ignores the special cases.
What am I doing wrong?
According to the apache docu the syntax is as folows:
Redirect 301 /service http://foo2.bar.com/service
So try:
Redirect 301 /pi/Dækkape_UG150_12_lysegrå_5302_.aspx http://www.el-netsalg.dk/pi/Dækkape_UG150_12_lysegrå_5271_.aspx
without the "http://www.el-netshop.dk" for the old-path paramater.

301 redirect .htaccess

Im trying to request the following entire site 301 redirect:
word.something.blah.domain.com --> http://www.word.com
I don't know how to write the 301 redirect rule.
Can someone help out?
I will assume you are using the same directory to serve files on both domains. In which case, a Redirect clause won't work (infinite redirect loop).
With mod_rewrite, you can check the value of the current HTTP_HOST and take a decision based on that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.something\.blah\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.%1.com/$1 [R=301,NE,L]
put this into root directory of the subdomain:
Redirect permanent / http://www.word.com
If you are keeping everything else the same - that is, the file names - but simply changing the domain, this code is all you need to put on the OLD DOMAIN htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.newdomain\.co.uk
RewriteRule (.*) http://www.newdomain.co.uk/$1 [R=301,L]