htaccess Redirect everything except subdomain and folder - apache

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]

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 301 redirect not working correctly

I'm trying to setup an .htaccess file to redirect old urls which I have REMOVED the pages from the server (not sure if this is the problem). I'm trying to redirect the pages to the new page which has the same content. Can anyone tell me why this doesn't work? (I believe it should) followed several online guides.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com/wheels_landing_page.php [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com/wheels_landing_page.php [NC]
RewriteRule ^(.*)$ http://example.com/wheelsLanding.php/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^example.com/dealerships.php [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com/dealerships.php [NC]
RewriteRule ^(.*)$ http://example.com/contact.php/$1 [L,R=301,NC]
Try with:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^wheels_landing_page\.php(.*)$ http://example.com/wheelsLanding.php$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^dealerships\.php(.*)$ http://example.com/contact.php$1 [L,R=301,NC]

.htaccess rules causing internal server error yet they look right

My server's. htaccess rules look like this:
RewriteEngine On
# first redirect
RewriteCond %{HTTP_HOST} ^www\.testing\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^testing\.examplewebsite\.co\uk$ [NC]
RewriteRule ^(.*)$ https://testing.examplewebsite.com/$1 [L,R=301]
# second redirect
RewriteCond %{HTTP_HOST} ^www\.test\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^test\.examplewebsite\.co\uk$ [NC]
RewriteRule ^(.*)$ https://test.examplewebsite.com/$1 [L,R=301]
# Main redirect
RewriteCond %{HTTP_HOST} ^www\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^examplewebsite\.co\uk$ [NC]
RewriteRule ^(.*)$ https://examplewebsite.com/$1 [L,R=301]
# All subdomains that don't exist redirect
RewriteCond %{HTTP_HOST} ^(.+)\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^ https://www.examplewebsite.com/ [L,R]
But for some reason none it causes a server internal error, I'm not sure if they are all 100% correct either. Like the last rule, should that too use ^(.*)$ at the start and $1 at the end
Same for the flags, not too sure if they are all correct.
Basically I am trying to make:
testing.examplewebsite.co.uk and www.testing.examplewebsite.co.uk go to https://testing.examplewebsite.com
test.examplewebsite.co.uk and www.test.examplewebsite.co.uk go to https://test.examplewebsite.com
Some other subdomains like above
examplewebsite.co.uk and www.examplewebsite.co.uk go to https://examplewebsite.com
Make all used domains, for example stackoverflow.examplewebsite.co.uk and www.stackoverflow.examplewebsite.co.uk both be sent to https://examplewebsite.com
You have a typo in all of your conditions. the 2nd conditions in your lines is missing a . before uk. ^testing.examplewebsite.co.uk$ [NC]
RewriteEngine On
# first redirect
RewriteCond %{HTTP_HOST} ^www\.testing\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^testing\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://testing.examplewebsite.com/$1 [L,R=301]
# second redirect
RewriteCond %{HTTP_HOST} ^www\.test\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^test\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://test.examplewebsite.com/$1 [L,R=301]
# Main redirect
RewriteCond %{HTTP_HOST} ^www\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^examplewebsite\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://examplewebsite.com/$1 [L,R=301]
# All subdomains that don't exist redirect
RewriteCond %{HTTP_HOST} ^(.+)\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^ https://www.examplewebsite.com/ [L,R]

Rewrite all subdomain URLs with .htaccess

I have a domain test.com and a subdomain sub.test.com.
Now, I need to rewrite the URL sub.test.com to test.com/?state=sub, and the URLs like sub.test.com/content/ to test.com/content/?state=sub, and sub.test.com/content/proceed/ to test.com/content/proceed/?state=sub.
The rule for the first URL worked, something like this:
RewriteCond %{HTTP_HOST} !^www\.test\.com
RewriteCond %{HTTP_HOST} !^webmail\.test\.com
RewriteCond %{HTTP_HOST} !^m\.test\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.test\.com
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !robots.txt.*
RewriteRule ^$ http://www.test.com/?state=%1 [P]
Any idea how to do this?
This rule should work for you:
RewriteCond %{HTTP_HOST} !^(?:www|webmail|m)\.test\.com$ [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.test\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(index\.php|robots\.txt) [NC]
RewriteRule ^ http://www.test.com%{REQUEST_URI}/?state=%1 [P]

Redirect from few url's (both www and non-www) to original one

How to shorten this expression? i want instead of using 2 lines just check whatever domain is something different from mysite.com e.g. mysite.org or www.mysite.org with or without www and redirect.
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^mysite\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.org$ [OR]
RewriteCond %{HTTP_HOST} ^mysite\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.net$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
Also is it faster to use [OR] or do like this
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite\.org$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.mysite\.org$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite\.net$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
P.S. why this works as case insensetive even when i dont use [NC]?
You may want to check Apache documentation on canonical hostnames, your rules can be expressed as a negation
RewriteEngine On
RewriteCond %{HTTP_HOST} !^mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]
And for the rules matching without [NC] , it probably depends on what the browser sends to the server.