.htaccess rules causing internal server error yet they look right - apache

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]

Related

Wildcard 301 redirect using .htaccess

I moved a website from http://lifeworkslearningcenter.com to http://lifeworks.life. I set up seven 301 redirects for individual URLs that are working fine, as well as a simple 301 to redirect the base URL to the new base URL.
Now I need to set up a wildcard for the remaining URLs, typos, etc. What is happening right now is this: any URL not specifically covered in the 7 redirects will redirect to the new base URL, then add the previously entered trailing URL, and results in a 404. Like this:
http://lifeworkslearningcenter.com/incorrect-url
https://www.lifeworks.life/incorrect-url
Here is my .htaccess code, including three previous attempts at wildcard redirect statements:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^about-lifeworks$ "https\:\/\/www\.lifeworks\.life\/team\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^services$ "https\:\/\/www\.lifeworks\.life\/services\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^methodology$ "https\:\/\/www\.lifeworks\.life\/approach\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^sign-up$ "https\:\/\/www\.lifeworks\.life\/enroll\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^rates-and-policies$ "https\:\/\/www\.lifeworks\.life\/policies\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^contact-us$ "https\:\/\/www\.lifeworks\.life\/contact\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^blog$ "https\:\/\/www\.lifeworks\.life\/blog\.html" [R=301,L]
Redirect 301 / http://lifeworks.life/
# FAILED ATTEMPTS AT WILDCARD REDIRECT
#RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
#RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
#RewriteRule ^$ "https\:\/\/www\.lifeworks\.life\/index\.html" [R=301,L]
#RedirectMatch 301 /(.*) /$1
#RedirectMatch ^/(.*)$ http://lifeworks.life/$1
Your wildcard redirect should look like this and placed at the bottom of your other rules.
RewriteRule ^(.*)$ https://www.lifeworks.life/$1 [R=301,L]
If you need it to redirect based on file not found (404) you can have these rules. And place these at the bottom of your rules.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://www.lifeworks.life/$1 [R=301,L]

mod_rewrite rules for my site

I have these rules on my site:
RewriteCond $1 !^(support|about-us|profile|support|features|videos|terms-of-service|about-us|signup|media|includes|modules|cgi-bin|templates|xmlrpc|language|libraries|plugins|administrator|component|images|dev|data)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([a-z0-9\-]+)/[^\ ]*\ HTTP/
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^([a-z0-9\-]+)/(.*)$ https://$1.example.com/$2 [R=301,L]
#
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.example\.com
RewriteCond %1 !^www\.
RewriteRule ^(support|about-us|profile|support|features|newserver|videos|about-us|terms-of-service|signup|component|includes|media|cgi-bin|templates|xmlrpc|language|modules|libraries|plugins|administrator|images(/.*))$ https://example.com/$1 [R=301,L]
# Externally redirect all www hostnames to non-www hostnames
RewriteCond %{HTTP_HOST} ^(([a-z0-9\-]+\.)*)www\.(([a-z0-9\-]+\.)*)example\.com
RewriteRule ^(.*)$ https://%1%3example.com/$1 [R=301,L]
I added these below to redirect these types: https://example.com/firstnamelastname?mv=0 etc
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^mv=[0-4]$
RewriteRule ^([A-Za-z0-9]+)$ https://$1.example.com/? [L,R]
However I cannot get these to redirect https://example.com/firstnamelastname
If i remove the Query string from the last 3 lines then If i would click on features link on my website It would redirect to https://features.example.com which is what the rules at the top are preventing.
Can anyone assist with this.

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]

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.

redirecting file request for subdomain and main site with certain arguments?

I am running wildcard subdomains. the aim of the site is to run a virtual subdomain. what the functionality should be is that when the files
from the main site are called they will be rewritten for the clean URLs. But when i am calling the same files form the subdomain
like xyz.doamin.com/my-file.php then this should be rewritten like it is calling that file with an argument of subdoamin like
domain.com/my-file.php?var=xyz. Currently what i have done for the file call is this
Options +FollowSymLinks
RewriteEngine on
## Redirecting all non-www domains to www.domain.com
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?var=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&var=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://www.domain.com/index.php?subdomain=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&subdomain=%1 [L]
This is throwing a 500 internal error for the file call.
Rule set 4 will overlap with rule set 1 and 2. I suspect this is causing the issue.
So I think all 3 need an extra RewriteCond check on REQUEST_URI.
Really you shouldn't need rule set 4. Just including index.php as a default should do the trick. Unless of course you don't want URL's like http://www.domain.com/?var=xyz, in this case you need rule 4, but you need to split it into 2 - one with a QUERY_STRING and one without, just like rule 1 and 2
Also, isn't %1 from the regex in the rewriteRule itself and not the preceeding rewriteConds? If so then your regex is wrong, because it'll be setting the var= to the entire original URL.
Looking in the error log is a good idea as it will give you the reason for the 500 error.
Edit: Ok, try something like this (not tested)
## Redirecting all non-www domains to www.domain.com
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L,QSA]
## Redirecting / requests to index.php
# Note: Use the QSA flag for handling with or without query string
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/index.php?var=%1 [L,QSA]
## Redirecting /something requests to index.php
# Note: Use the QSA flag for handling with or without query string
RewriteCond %{REQUEST_URI} ^/.+$
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?var=%1 [L,QSA]