How can I load content from two different folders (based on the subdomain) and keep the old URLs?
My website is example.com.
In my public_html folder, I have two folders. One is called main, and the other is called report.
When somebody navigates to example.com, I want to serve content from the main folder, but I want their URL to remain the same.
So, if they navigate to example.com/file.html, I want them to see the file.html file that's in the main folder, but I don't want their URL to change to example.com/main/file.html.
Similarly, if they navigate to report.example.com/report1.pdf, I want them to see the report1.pdf file that's in the report folder, but I don't want their URL to change to report.example.com/report/report1.pdf.
The closest thing I've been able to achieve is this:
DirectoryIndex index.php index.html index.htm
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^((?!main).*)$ /main/$1
RewriteCond %{HTTP_HOST} ^report.example.com$ [NC]
RewriteRule ^((?!report).*)$ /report/$1 [L]
That seems to work to some extent, but:
If somebody navigates to
http://example.com/report/index.html, the server incorrectly loads theindex.htmlfile from thepublic_html/reportfolder rather than thepublic_html/main/reportfolder. Normally that path would make sense, but in this case I'm trying to change that behavior.If somebody navigates to
http://report.example.com/test, the server correctly displays thepublic_html/test/index.htmlfile, but redirects them tohttp://report.example.com/report/test/.
How can I load the correct files and keep the URLs correct?
regex apache .htaccess url-rewriting subdomain
add a comment |
My website is example.com.
In my public_html folder, I have two folders. One is called main, and the other is called report.
When somebody navigates to example.com, I want to serve content from the main folder, but I want their URL to remain the same.
So, if they navigate to example.com/file.html, I want them to see the file.html file that's in the main folder, but I don't want their URL to change to example.com/main/file.html.
Similarly, if they navigate to report.example.com/report1.pdf, I want them to see the report1.pdf file that's in the report folder, but I don't want their URL to change to report.example.com/report/report1.pdf.
The closest thing I've been able to achieve is this:
DirectoryIndex index.php index.html index.htm
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^((?!main).*)$ /main/$1
RewriteCond %{HTTP_HOST} ^report.example.com$ [NC]
RewriteRule ^((?!report).*)$ /report/$1 [L]
That seems to work to some extent, but:
If somebody navigates to
http://example.com/report/index.html, the server incorrectly loads theindex.htmlfile from thepublic_html/reportfolder rather than thepublic_html/main/reportfolder. Normally that path would make sense, but in this case I'm trying to change that behavior.If somebody navigates to
http://report.example.com/test, the server correctly displays thepublic_html/test/index.htmlfile, but redirects them tohttp://report.example.com/report/test/.
How can I load the correct files and keep the URLs correct?
regex apache .htaccess url-rewriting subdomain
Do you have access to apache configuration files, or.htaccessis the only option?
– Dusan Bajic
Jan 1 at 11:22
@DusanBajic - We're using Cloudways, so we have some options but not total control.
– Pikamander2
Jan 2 at 2:32
add a comment |
My website is example.com.
In my public_html folder, I have two folders. One is called main, and the other is called report.
When somebody navigates to example.com, I want to serve content from the main folder, but I want their URL to remain the same.
So, if they navigate to example.com/file.html, I want them to see the file.html file that's in the main folder, but I don't want their URL to change to example.com/main/file.html.
Similarly, if they navigate to report.example.com/report1.pdf, I want them to see the report1.pdf file that's in the report folder, but I don't want their URL to change to report.example.com/report/report1.pdf.
The closest thing I've been able to achieve is this:
DirectoryIndex index.php index.html index.htm
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^((?!main).*)$ /main/$1
RewriteCond %{HTTP_HOST} ^report.example.com$ [NC]
RewriteRule ^((?!report).*)$ /report/$1 [L]
That seems to work to some extent, but:
If somebody navigates to
http://example.com/report/index.html, the server incorrectly loads theindex.htmlfile from thepublic_html/reportfolder rather than thepublic_html/main/reportfolder. Normally that path would make sense, but in this case I'm trying to change that behavior.If somebody navigates to
http://report.example.com/test, the server correctly displays thepublic_html/test/index.htmlfile, but redirects them tohttp://report.example.com/report/test/.
How can I load the correct files and keep the URLs correct?
regex apache .htaccess url-rewriting subdomain
My website is example.com.
In my public_html folder, I have two folders. One is called main, and the other is called report.
When somebody navigates to example.com, I want to serve content from the main folder, but I want their URL to remain the same.
So, if they navigate to example.com/file.html, I want them to see the file.html file that's in the main folder, but I don't want their URL to change to example.com/main/file.html.
Similarly, if they navigate to report.example.com/report1.pdf, I want them to see the report1.pdf file that's in the report folder, but I don't want their URL to change to report.example.com/report/report1.pdf.
The closest thing I've been able to achieve is this:
DirectoryIndex index.php index.html index.htm
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^((?!main).*)$ /main/$1
RewriteCond %{HTTP_HOST} ^report.example.com$ [NC]
RewriteRule ^((?!report).*)$ /report/$1 [L]
That seems to work to some extent, but:
If somebody navigates to
http://example.com/report/index.html, the server incorrectly loads theindex.htmlfile from thepublic_html/reportfolder rather than thepublic_html/main/reportfolder. Normally that path would make sense, but in this case I'm trying to change that behavior.If somebody navigates to
http://report.example.com/test, the server correctly displays thepublic_html/test/index.htmlfile, but redirects them tohttp://report.example.com/report/test/.
How can I load the correct files and keep the URLs correct?
regex apache .htaccess url-rewriting subdomain
regex apache .htaccess url-rewriting subdomain
asked Jan 1 at 6:33
Pikamander2Pikamander2
2,13722134
2,13722134
Do you have access to apache configuration files, or.htaccessis the only option?
– Dusan Bajic
Jan 1 at 11:22
@DusanBajic - We're using Cloudways, so we have some options but not total control.
– Pikamander2
Jan 2 at 2:32
add a comment |
Do you have access to apache configuration files, or.htaccessis the only option?
– Dusan Bajic
Jan 1 at 11:22
@DusanBajic - We're using Cloudways, so we have some options but not total control.
– Pikamander2
Jan 2 at 2:32
Do you have access to apache configuration files, or
.htaccess is the only option?– Dusan Bajic
Jan 1 at 11:22
Do you have access to apache configuration files, or
.htaccess is the only option?– Dusan Bajic
Jan 1 at 11:22
@DusanBajic - We're using Cloudways, so we have some options but not total control.
– Pikamander2
Jan 2 at 2:32
@DusanBajic - We're using Cloudways, so we have some options but not total control.
– Pikamander2
Jan 2 at 2:32
add a comment |
1 Answer
1
active
oldest
votes
This should do:
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^ /main%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^reports.example.com$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^ /reports%{REQUEST_URI} [L]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993488%2fhow-can-i-load-content-from-two-different-folders-based-on-the-subdomain-and-k%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This should do:
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^ /main%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^reports.example.com$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^ /reports%{REQUEST_URI} [L]
add a comment |
This should do:
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^ /main%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^reports.example.com$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^ /reports%{REQUEST_URI} [L]
add a comment |
This should do:
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^ /main%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^reports.example.com$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^ /reports%{REQUEST_URI} [L]
This should do:
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^ /main%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^reports.example.com$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^ /reports%{REQUEST_URI} [L]
answered Jan 2 at 9:25
Dusan BajicDusan Bajic
6,10831826
6,10831826
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993488%2fhow-can-i-load-content-from-two-different-folders-based-on-the-subdomain-and-k%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you have access to apache configuration files, or
.htaccessis the only option?– Dusan Bajic
Jan 1 at 11:22
@DusanBajic - We're using Cloudways, so we have some options but not total control.
– Pikamander2
Jan 2 at 2:32