htaccess redirect www (es) to non www (es) - apache

I'm currently redirecting all requests for www.example.com to example.com.
I have this working nicely with this code found elsewhere on stackoverflow:
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
So far so good.
The problem I have is that I also need to ensure that the Spanish version of my site also redirects — www.example.com/es needs to redirect to example.com/es, but this doesn't work with the code shown above.
What would I need to add to make the redirection work?
Thanks for your help!

Inside your example.com/es/ folder, create a .htaccess file with the following contents:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/es/$1 [L,R=301]
(Reference)

Related

RewriteCond for a folder only on a specific domain extension

I have a site that can be accessed via 3 domains (domain.com, domain.ch, domain.fr). The three use exactly the same files and folder, though.
Regarding the .fr domain (and only this domain), I need the following:
redirect domain.fr (root) to domain.com/fr/france.
This has been achieved with the following rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/fr\/france" [R=301,L]
It works. (There's also a functioning rule to force www. in front of every URL.)
What I can't get to work is:
redirect domain.fr/fr also to domain.com/fr/france.
and finally, redirect any URL domain.fr/fr/* to domain.com/fr/*
(keeping whatever * stands for).
The trick (to me) is that the same .htaccess file will also be present on domain.com and domain.ch, but those rules must not activate for those domains.
You can put these rules in your htaccess
# Redirect [www.]domain.fr and [www.]domain.fr/fr to www.domain.com/fr/france
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^(|fr)$ http://www.domain.com/fr/france [R=301,L]
# Redirect domain.fr/fr/* to domain.com/fr/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/$1 [R=301,L]
Try :
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteCond %{REQUEST_URI} !^/fr/france
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/france/$1 [R=301,L]

Issue with redirecting to subdomain .htaccess

I am having an issue with .htaccess. I got 2 websites one is in main root folder other is in root/subdomain.
I got .htaccess for handling subdomain:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /sub.domain.com/ [R=301,L]
Problem which I met is that url looks like:
https://sub.domain.com/sub.domain.com/
I want to hide the sub.domain.com via .htaccess.
Also I want to mention that everything works correctly on http.
Could you help me ?
Can you please try to add following code in your sub-domain .htaccess file and check it again
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

RewriteRule showing inconsistence behavior

I have the following rule in my htaccess file (this is the full htaccess file):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?!www\.).+$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST} [L,R=301]
Using the htaccess tester (http://htaccess.madewithlove.be/) it shows the non-www URL is correctly redirected to a www url (test with mydomain.com/ and mydomain.com/subdirectory/ (goes to www.mydomain.com/subdirectory/).
Now, when I put this htaccess file on my site, it will redirect mydomain.com/subdirectory/ to www.mydomain.com instead of to www.mydomain.com/subdirectory/
Why does it show this inconsistent behavior?
You're not using capture value $1 in target:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
So, I looked up what the HTTP_HOST value actually is. It looks like it doesn't contain any further than the domain extension (i.e. domain.com but not anything that's behind that) so it will redirect to domain.com)
I modified the htaccess to the following, which is working:
RewriteCond %{HTTP_HOST} ^(?!www\.).+$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If anyone still has any suggestions how to improve the regex, I'm open for improvements

.htaccess issue with using a -org secondary domain vs using a .com secondary domain

I have successfully forwarded www.atlantalawvideo.legalvideoconsulting.com to www.atlantalawvideo.com for SEO purposes.
I'm using this code in my .htaccess file in my /atlantalawvideo/ folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.atlantalawvideo.com
RewriteRule (.*) http://www.atlantalawvideo.com/$1 [R=301,L]
I'm having trouble forwarding www.legalvideographer-org.legalvideoconsulting.com to www.legalvideographer.org
I'm using this code in my .htaccess file in my /legalvideographer-org/ folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.legalvideographer.org
RewriteRule (.*) http://www.legalvideographer.org/$1 [R=301,L]
Not sure what the problem is http://www.legalvideographer-org.legalvideoconsulting.com forward goes nowhere.
The second rewrite is not working because you are not meeting the condition of the %{HTTP_HOST}. You're not using the full host name to check. legalvideographer.org appears to be a subdomain oflegalvideoconsulting.com. See if this works for you.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.legalvideographer-org\.legalvideoconsulting\.com [NC]
RewriteRule (.*) http://www.legalvideographer.org/$1 [R=301,L]
That should work if that is your domain name.
You should also be able to do a simple redirect instead of the rewrite rule
redirct 301 / http://www.legalvideographer.org

RewriteRule help

I have a URL
http://test.devsite-1.com/test/tbox/
which I want to redirect to
http://tbox.devsite-1.com/
Rule:
RewriteCond %{HTTP_HOST} !^tbox\.(.*)$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.|)(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/tbox(/.*|)$
RewriteRule /tbox/(.*) http://tbox.%{HTTP_HOST}/$1 [R=301,L]
I don't understand why it is not redirecting me to the URL? Please note I need a generalized rule so that if I change test.devsite-1.com to tempo.devsite-1.com the same should work with the other URL as well.
Try this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.(.+)$ [NC]
RewriteRule ^test/tbox/(.*)$ http://tbox.%1/$1 [R=301,L]
This will redirect (301 Permanent Redirect)
http://test.devsite-1.com/test/tbox/something-optional
to
http://tbox.devsite-1.com/something-optional
It has to be placed in .htaccess file in website root folder (e.g. http://test.devsite-1.com/.htaccess). If placed elsewhere some tweaking may be required.
It will only work if request is coming via test. subdomain.
And it will only work if URL requested starts with test/tbox/.
All of the above matches your URL examples.