301 redirect not working properly (my guess is b/c I'm dealing with Add-on domains) - apache

I have a primary/account domain (PrimaryDomain.com) and a couple add-on domains (AddOnDomain1.com and AddOnDomain2.com) I've recently switched the content from AddOnDomain1.com to AddOnDomain2.com and I want everything to redirect to the exact same location (folders, files, images, etc.)
Here's what I have in .htaccess of my public_html folder (for PrimaryDomain.com):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.AddOnDomain1\.com$
RewriteRule (.*) http://AddOnDomain2.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^AddOnDomain1\.com$
RewriteRule (.*) http://AddOnDomain2.com/$1 [R=301,L]
It trys to redirect but here's what happens (with an image as an example). When I enter:
http://AddOnDomain1.com/img/logo.png
It redirects to:
http://AddOnDomain2.com/AddOnDomain1/img/logo.png
Notice how it adds that folder (in bold)?...which of course is the add-on domain directory name. Anyone aware of a fix for this scenario?

Related

redirect everything on the site except one folder and its subfolders

I have a site, and I would like to forward everything expect one folder and it's subfolders
I need this for my .htaccess on Linux Apache
so for example everything under
www.mysite.com
www.mysite.com/random_folder
www.mysite.com/random_folder2
goes to extra domain
www.extradomain.com
but
www.mysite.com/except_me_folder
www.mysite.com/except_me_folder/subfolder
www.mysite.com/except_me_folder/?anyrequest
have no redirection
I tried this
RewriteCond %{REQUEST_URI} !^/except_me_folder/?.*$ [NC]
RewriteRule (.*) https://extradomain/$1 [R=301,L]

.htaccess rewrite all contents from subdomain to domain

I rewrited a subdomain to a section on my site:
https://music.example.com points to https://example.com/section.php?url=music
RewriteCond %{HTTP_HOST} ^music.example.com
RewriteRule ^(.*)$ section.php?url=music/$1 [L,NC,QSA]
But I need to use the same images, css, etc. for this (and other) subdomains.
https://example.com/images/img.jpg
Should be accessible from https://music.example.com/images/img.jpg
I tried to find a solution but I might not be doing the right search.
RewriteCond %{HTTP_HOST} ^music.example.com
RewriteRule ^(.*)$ section.php?url=music/$1 [L,NC,QSA]
This rule internally rewrites (not "redirects") a request of the form music.example.com/<anything> to section.php?url=music/<anything>. ie. Everything, including all images and CSS etc. This is much more than stated in your description, in which you only want the root directory to "point to" https://example.com/section.php?url=music.
If you only want the root directory to be rewritten then restrict the regex ^(.*)$ to only match the root.
For example:
RewriteCond %{HTTP_HOST} ^music\.example\.com
RewriteRule ^$ section.php?url=music [QSA,L]
NB: Don't forget to backslash-escape the literal dots in the regex.
This now naturally excludes everything else from being rewritten, except for requests to the subdomain's root directory, ie. https://music.example.com/ only.
.htaccess redirect all contents from subdomain to domain
As noted above, this is an internal "rewrite", not an external "redirect". And neither does this rewrite from subdomain to domain. The requested hostname does not change and it is assumed that both the subdomain and domain apex point to the same place on the filesystem.

Why is the htaccess in my subdomain folder overriding aspects of the htaccess in my main domain?

Let me explain my setup here, I have two domains, for the sake of naming them lets call them domain1.com and domain2.com. I have shared hosting that runs cPanel.
Domain1.com is my main domain, and is what I have my hosting account setup using. I have a website hosted at that domain. Domain2.com is set as an add-on domain, and directs to a folder inside of the main domains root. That has it's own website (a Ghost blog).
That all works fine. However the websites hosted at the two domains are quite different and I did not want the subdomain to work, but as far as I can tell add-on domains in cPanel have to have a subdomain in order to be added to the account. The redirect options for the subdomains in cPanel aren't good enough for me, as I wanted anyone accessing say domain1.com/domain2 to get a 404 error as if it didn't exist. So I set up the .htaccess file for domain1.com to look like this:
RewriteEngine On
#301 (permenant) redirects all HTTP requests to HTTPS (SSL)
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#404 redirects all domain.com/subdomain requests
RewriteCond %{HTTP_HOST} ^(www.)?domain1.com$ [NC]
RewriteCond %{REQUEST_URI} ^/domain2/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
#404 redirects all subdomain.domain.com & www.subdomain.domain.com requests
RewriteCond %{HTTP_HOST} ^domain2.domain1.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain2.domain1.co.uk$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ - [L,R=404]
This worked perfectly, anyone tryting to access domain2 via domain1 would get a 404 error. But that was before I installed Ghost on domain2 After getting Ghost installed I created an .htaccess file in the domain2 folder inside of domain1's root.I had to put this in it in order for Ghost for to run, as it uses NodeJS:
RewriteEngine on
RewriteRule ^(.*)$ http://127.0.0.1:55555555/$1 [P,L]
This also works perfectly except for one aspect. Now all requests to the domain using domain1.com have started working again. For example if I type domain2.domain1.com it will now show me the Ghost blog where as before it was correctly displaying a 404 Not Found error. Why is this and how do I go about rectifying this issue?
As a side note, I also started trying to have all http requests redirect to https. I added the following to the domain1.com htaccess file as I wanted all requests on both domains to redirect to https and I assumed this was necessary:
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Now this kind of works. All requests, whatever they are (with or without www for instance), redirect to https, and when I visit it in my browser I get a green the icon indicating it's secured with SSL. Again even with this the previous issue of the domain redirect still doesn't work. But I have another odd issue here.
If I type in my browser simply "domain1.com" (ie without https:// or www. or a combination of the two), it will show just "domain1.com" in the address bar but with the green icon and say it is secured. If however I type in "domain2.com" (again with no https:// or www etc.) it does the same thing except it states it is not secure, suggesting it requested http.
And yet if I type in www.domain2.com or http://domain2.com, that works and I get the secured icon. It is highly puzzling. It seems that the https redirect works on every request except for just "domain2.com" while any other variation (www.domain2.com, http://domain2.com etc) works fine. Any clues? I mean clearly I've done something wrong with the htaccess files but I don't know what, I got most of what I'd put in them from searching sites like Stackexchange, but personally I don't really know anything about them or how they work.
I think I have fixed my own problem here.
I corrected the main htaccess so the https redirect acts like this;
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
From what I can tell it's better to do it this way than the way I had it previously. This in itself didn't fix the problems. What fixed it for me is editing the domain2.com htaccess to look like this:
RewriteEngine on
#Force WWW if it isn't in the request
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
#Or include WWW even if it is in the request
RewriteCond %{HTTP_HOST} ^www\. [NC]
#Then Force it to use https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Defines this domain so main htaccess rules for subdomains work
RewriteCond %{HTTP_HOST} ^(.*)?domain2.com$ [NC]
#Proxy for Ghost
RewriteRule ^(.*)$ http://127.0.0.1:55555555/$1 [P,L]
Now everything works. All requests are redirected to https. All requests to the subdomain from the main domain fail with a 404. domain2.com now redirects to https://www.domain2.com or https://domain2.com so it is now always secured by SSL. I'm a novice at htaccess as is probably evident and I've no doubt some or all of this could have unnecessary elements removed or simplified so if anyone wants to correct my syntax so it's less messy I'd appreciate it.

Apache mod_rewrite from a folder to a different domain doesn't work with trailing slash

I'm trying to rewrite URLs from old.domain.tld/project to domain.tld/subfolder/project using .htaccess in the project directory on the old server. I think I need to check for the host name so the rule only applies to the old server, even if the .htaccess file is also put on the new server.
I've tried the following .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.domain\.tld$ [NC]
RewriteRule ^(.*)$ https://domain.tld/subfolder/project/$1 [R=301,L]
This works fine for URLs like https://old.domain.tld/project/index.php?p=q (redirects to https://domain.tld/subfolder/project/index.php?p=q) but not for the URL without the trailing slash—https://old.domain.tld/project ends up being redirected to https://domain.tld. Very odd! How do I make this work for both types of URL?
I tried a rather different method to make this a generic redirect, that could be included even if the folder name wasn't project, but this has the same problem:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.domain\.tld$ [NC]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule . https://domain.tld/subfolder/%1 [R=301,L]
In a server-wide configuration, a slash is appended to all requests for index files, e.g. old.domain.tld/project is redirected to old.domain.tld/project/. Could this be causing a problem?

Apache mod_rewrite fake pushState directory mapping

If have played around a lot with mod_rewrite rules in my httpd.conf file. Regardless of my research i haven't been able to get a couple of things working.
This is my file structure:
/
-index.php
-app.php
/css
-style.css
/js
-script.js
The server should either serve the index.php (home page) or app.php (single application page). Both the script and style files are included in both php files.
Goals
My domain domain.com should serve the index.php, the address bar should show www.domain.com
(This seems to work already, per default.)
The subdomain domain.com/a should be changed to domain.com/a/ if necessary. This domain should serve the app.php file.
(This is already working to an extend. One problem is that the relative links inside app.php are wrong, because the file "thinks" it is in a subdirectory instead of root. This I would like to change)
Anything after domain.com/a/ e.g. domain.com/a/user/10 should stay in the address bar and serve the app.php as usual. Ideally, to preserve relative links again, the file should "know" it is in the root folder.
(This is in order to support a "fake" pushState server) EDIT Clarification: Everything after the /a/ will be interpreted by my Javascript app. When the client clicks a link like domain.com/a/user/10 there will be no extra request to the server.
Bonus
Add trailing slashes to all URLs except the root url.
e.g. turn domain.com/a/user/10 into domain.com/a/user/10/
Add www to URL in case it is missing.
What I've tried
-add www
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]
-add slashes
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301]
-redirect /a/
RewriteCond %{REQUEST_URI} ^(/a/)$
RewriteRule ^ /app.php
Any pointers or help are greatly appreciated! Thanks.
Edit
I have used this tool http://htaccess.madewithlove.be/ to test some conditions.