Merge htaccess HTTP_HOST Rewrite Cond - apache

i have this htaccess conditions...
# changed... subdomains to subdirectories...
RewriteCond %{HTTP_HOST} ^acoruna.domain\.es [nc]
RewriteRule (.*) http://domain.es/acoruna/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^alava.domain\.es [nc]
RewriteRule (.*) http://domain.es/alava/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^albacete.domain\.es [nc]
RewriteRule (.*) http://domain.es/albacete/$1 [R=301,L]
# changed... subdomains to subdirectories...
My question is,, can i merge all that RewriteCond & Rewrite Rules, in only 1? because all do the same.
Thanks, regards from Spain.

Try:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.es$ [NC]
RewriteRule ^(.*)$ http://domain.es/%1/$1 [R=301,L]

Related

Multiple domains on one host https and http

I have multiple domains on one host, my .htaccess looks like this
RewriteEngine On
RewriteCond %{HTTP_HOST} web1.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_1/public/.*$
RewriteRule ^(.*)$ /web_1/public/$1 [L]
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_2/public/.*$
RewriteRule ^(.*)$ /web_2/public/$1 [L]
And it works so far. Now I want only web1.com, not web2.com, to redirect to https. How do I have to change the settings? (I am also thankful for tips about how to change my "implementation" so far, I am new to .htaccess and willing to learn more about it)
Try this :
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
So , your rules will look like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} web1.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_1/public/.*$
RewriteRule ^(.*)$ /web_1/public/$1 [L]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_2/public/.*$
RewriteRule ^(.*)$ /web_2/public/$1 [L]

.htaccess domain-1/directory-x to domain2.directory-y

How would I, with .htaccess, do the following
RewriteCond %{HTTP_HOST} ^(www.)?domain-1\.com$ [NC]
RewriteCond %{REQUEST_URI} /posts/([a-z0-9_]+)
RewriteRule ^(.*)$ https://www.domain-2\.com/articles/$1 [L,R=301]
So that "/posts/" does not become part of the forwarded path?
Example, I want
http://www.domain-1.com/posts/2014/06/17/some-post-title
To forward to
https://www.domain-2.com/articles/2014/06/17/some-post-title
Thank you for any help.
You can use:
RewriteCond %{HTTP_HOST} ^(www\.)?domain-1\.com$ [NC]
RewriteRule ^posts/(.*)$ https://www.domain-2.com/articles/$1 [L,R=301,NE]

htaccess Redirect everything except subdomain and folder

I am trying to redirect my whole domain www.example.de to www.domain.de/artikel.html except for 3 subdomains (and all whats in there) and 2 folders (and everything whats in there):
RewriteCond %{REQUEST_URI} !^/folder1/subfolder2/?$ [NC]
RewriteCond %{REQUEST_URI} !^/folder2/?$ [NC]
RewriteCond %{HTTP_HOST} !^static1\.example\.de$ [NC]
RewriteCond %{HTTP_HOST} !^static2\.example\.de$ [NC]
RewriteCond %{HTTP_HOST} !^static3\.example\.de$ [NC]
RewriteCond %{HTTP_HOST} ^(\w+)\.example\.de [NC]
RewriteRule ^(.*)$ http://www.domain.de/artikel.html [R=301,L]
But it just redirects everything with www.example.de to www.domain.de/artikel.html but does not redirect anything for http://example.de.
Anyone a hint or solution for me??
Thanks!
the problem is with this condition which causes redirections to occur only for subdomains:
RewriteCond %{HTTP_HOST} ^(\w+)\.example\.de [NC]
replace it with :
RewriteCond %{HTTP_HOST} (\.|^)example\.de [NC]
or you can completely remove it if your htaccess is attached to only one site (example.de)
your htaccess would be:
RewriteCond %{REQUEST_URI} !^/folder1/subfolder2/?.*$ [NC]
RewriteCond %{REQUEST_URI} !^/folder2/?.*$ [NC]
RewriteCond %{HTTP_HOST} !^static1\.example\.de$ [NC]
RewriteCond %{HTTP_HOST} !^static2\.example\.de$ [NC]
RewriteCond %{HTTP_HOST} !^static3\.example\.de$ [NC]
RewriteCond %{HTTP_HOST} (\.|^)example\.de [NC]
RewriteRule .* http://www.domain.de/artikel.html [R=301,L]

add www on http and remove www on https

I am trying to do the following:
add www to non secure
http://domain[.]com to http://www.domain[.]com
&
remomve www on secure
https://www[.]domain.com to https://domain[.]com
I am trying this but doesn't seem to work
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example.org$
RewriteRule ^(.*)$ http://www.example.org/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example.org$
RewriteRule ^(.*)$ https://example.org/$1 [R=301,L]
Try this:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.org$ [NC]
RewriteRule ^(.*)$ http://www.example.org/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.example\.org$ [NC]
RewriteRule ^(.*)$ https://example.org/$1 [R=301,L]
Your second HTTP_HOST condition still checks that the www. is missing, which should be the other way around.

force non www to www in htaccess

I have:
# BEGIN WithoutWWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
# END WithoutWWW
But it won't work. I can still access both www and non www versions. What am I doing wrong?
Try:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
//OR
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteRule ^(.*)$ [R=301,L]