.htaccess rewrite all contents from subdomain to domain - apache

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.

Related

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?

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

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?

Apache Mod_Rewrite needs to keep the Path but change the Domain

Background
I have a website that has several other alias domain names, in an effort to streamline and simplify I want all the domains to revert to the one domain name I now advertise -
Main name: www.explorerWorld.co.uk
Other names that load the same content but retain their own address:
www.exploreElsewhere.co.uk
www.exploitedexplodedexplorers.co.uk
what I have is that across the internet there are links in peoples blogs and elsewhere that go to specific pages on my site, but these will be under these different domains.
I have changed the domain Alises under cPanel and this works, for the base address only, but a domain for example www.exploreElsewhere.co.uk/trees.php does not redirect.
Question
I would like these pages to still work but to redirect to the main site -- www.explorerWorld.co.uk -- * but preserving their file path*
so: www.exploreElsewhere.co.uk/plants/trees.php gets seemlessly changed to www.explorerWorld.co.uk/plants/trees.php
My htaccess so far:
RewriteCond %{HTTP_HOST} !^exploreWorlds\.co\.uk$1 [NC]
RewriteRule ^(.*)$ http://www.exploreWorlds.co.uk/$1 [R=301,L]
But this seems to work in an endlessly repeating loop, How can I improve this htaccess ?
I have also looked here but this was not very helpful:
.htaccess change domain but keep path
I have found a solution to this problem was to use %{REQUEST_URI} which is the path given to the server.
(with a pointer from Arco444)
RewriteCond %{HTTP_HOST} !^(www\.)?exploreworlds\.co\.uk [NC]
RewriteRule ^(.*) http://www.exploreworlds.co.uk/%{REQUEST_URI} [R=301,L]
This successfully rewrites the domain part of the URL to force alias URL links to be redirected to my base website address + path
You have a mistake in your RewriteCond. Try
RewriteCond %{HTTP_HOST} !^www.exploreWorlds\.co\.uk$1 [NC]
RewriteRule ^(.*)$ http://www.exploreWorlds.co.uk/$1 [R=301,L]
instead, or remove the tilda from your original snippet

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.

.htaccess 301 redirects based on TLD

We're in the process of switching our current site from a multiple domain configuration into a single domain, multiple folder format. i.e.
.co.uk/<uri> is becoming .com/en-gb/<uri>
.com/<uri> is becoming .com/en-us/<uri>
I'm hoping that I'll be able to handle this via a couple of well-crafted .htaccess rules, but I'm not sure of code I'm going to need to achieve this. Can you help?
(PS, I've left the actual domain blank, as we only need to test for the TDL, not the entire domain - although whatever the original domain was need to stay the same, with only the TLD changing - i.e. whatever.co.uk would redirect to whatever.com/en-gb/, whatever2.co.uk would redirect to whatever2.com/en-gb/, and whatever.com would redirect to whatever.com/en-us/)
add the following directives to your .htaccess:
RewriteCond %{HTTP_HOST} (.+)\.co\.uk$
RewriteRule (.*) http://%1.com/en-gb/$1 [R=301,L,QSA]
RewriteCond %{HTTP_HOST} \.com$
RewriteCond %{REQUEST_URI} !^/(en-us|en-gb)/
RewriteRule (.*) /en-us/$1 [R=301,L,QSA]
if you have so many domain TLDs, you maybe want to use RewriteMap to avoid duplicating the first rule for every TLD, RewriteMap will map TLD to Uri string (ex: .co.uk to en-gb),