3

I want to redirect from example.org to subdomain.example.org

My domain is registered with GoDaddy and hosted on a StackCP server with a One-Click Install of Flarum.

If I go to example.org it takes me to subdomain.example.org/public_html/ where I get a notice saying The page you requested could not be found together with a link that takes me to subdomain.example.org which then correctly displays my forum.

So I tried modifying the .htaccess file in the root directory of my account by adding the following code from this answer here.

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^example.org [NC,OR] 
RewriteCond %{HTTP_HOST} ^www.example.org [NC] 
RewriteRule ^(.*)$ http://subdomain.example.org/$1 [L,R=301,NC]

However, doing so made no difference.

I added the code like this:

#+PHPVersion
#=(removed just in case)
AddHandler x-httpd-php83 .php
#-PHPVersion

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^example.org [NC,OR] 
RewriteCond %{HTTP_HOST} ^www.example.org [NC] 
RewriteRule ^(.*)$ http://forum.example.org/$1 [L,R=301,NC]

#+WebsiteRedirect
#=[]
RewriteEngine On
RewriteOptions InheritBefore
#-WebsiteRedirect

#+Unmarked
#EOF
#-Unmarked

#EOF

I also tried copying the modified .htaccess file to my public_html directory, but it made no difference.

Can anyone please help me with this?

6
  • Where did you add that code? What other directives do you have? (The code itself is OK.)
    – MrWhite
    Commented Aug 2 at 14:05
  • Where in the .htaccess file did you add it? Did you put it at the very top before any other directives? Commented Aug 2 at 14:37
  • 1
    “In my root directory” - root dir for your public HTML space or root directory of your “account”? What directives are you inheriting? (And why is the “inherited” code enclosed in a “WebsiteRedirect” comment?)
    – MrWhite
    Commented Aug 2 at 15:11
  • My bad: I've edited my question because I didn't realise that the redirect to subdomain.example.org/public_html/ happened before I modified the .htaccess file, and I hope I've answered all your other questions.
    – ontologist
    Commented Aug 2 at 15:58
  • 1
    Does the stackcp server support .htaccess files? .htaccess files are specific to Apache (with some support for lightspeed, but not NGINX), and can be disabled for performance and security reasons.
    – davidgo
    Commented Aug 2 at 23:57

2 Answers 2

5

Dealing with the redirect in .htaccess can be trickier than it seems at first. First off, make sure you've cleared your browser's cache completely, as sometimes cached data can hide the actual changes you've made.

If the redirect still isn’t working, it might be due to where your .htaccess file is located or how your server is configured. Double-check that the file is in the root directory of your domain, specifically in the public_html folder. Also, ensure that your server actually supports .htaccess directives, because if they’re disabled, any changes you make won’t take effect.

To improve the redirect, you might want to add the RewriteBase / directive before the redirect rules. This helps the server correctly interpret the paths.

Here’s an example of the code you could use in your .htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.org [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.org [NC]
RewriteRule ^(.*)$ http://subdomain.example.org/$1 [L,R=301,NC]

This code ensures that all requests to example.org are redirected to subdomain.example.org. If everything’s set up right, the redirect should work smoothly, automatically sending users to the correct subdomain without them needing to type it out manually.

Just remember, even small errors in configuration can lead to unexpected results, so double-check all your settings carefully!

1

I finally figured it out. The problem was that I couldn't find the file that held the text of the notice: The page you requested could not be found... so I couldn't see where to add the .htaccess file with the code.

The breakthrough came when I went to example.org in Firefox instead of Chrome and got a different notice: This site is brand new... I then realised that the other notice must have been from GoDaddy when I first bought the domain, and it was still stored in Chrome's cache.

The new notice was in the index.php file of my public_html folder, so it must have worked when I tried putting the .htaccess file there before, but I just didn't see it because of the cache.

Therefore, after learning how to clear the cache (doh!) I then created a new .htaccess file, inserted the code from my original question and it worked perfectly.

Many thanks to everyone who commented, and please excuse my ignorance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.