How to rewrite rules for sub-domains - .htaccess

I recently registered a domain name kbcsurveyors.com. Then, I created two sub-domains, which created two new folders in the root folder.
My motive is that if I type kbcsurveyors.com/preinspection, it should point to preinspection.kbcsurveyors.com. Same for other sub domains.
In my .htaccess, which I placed inside root of mydomainname.com, I have written following lines:
RewriteEngine On
RewriteBase /
RewriteRule ^preinspection/(.*)$   https://kbcsurveyors.com/$1 [L,NC,QSA]   # Handle requests for "Preinspection"
But this file structure does not work.
How do I write the .htaccess file to achieve what I want?
Regards

Remove or comment out your existing rule.
Create a new file preinspection/.htaccess with this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^preinspection\.kbcsurveyors\.com$ [NC]
RewriteEngine .* http://preinspection.kbcsurveyors.com/$0 [L,NE,R=301]

Related

Need help in URL rewriting sub-domains

I recently registered a domain name kbcsurveyors.com. Then, I created two sub-domains, which created two new folders in the root folder.
My motive is that if I type kbcsurveyors.com/preinspection, it should point to preinspection.kbcsurveyors.com. Same for other sub domains.
In my .htaccess, which I placed inside root of mydomainname.com, I have written following lines:
RewriteEngine On
RewriteBase /
RewriteRule ^preinspection/(.*)$   https://kbcsurveyors.com/$1 [L,NC,QSA]   # Handle requests for "Preinspection"
But this file structure does not work. How do I write the .htaccess file to achieve what I want?
Regards
EDIT
I have asked a fresh question as this one has been messed up. Here is the link:How to rewrite rules for sub-domains
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub-domain1.mydomainname.com
RewriteRule ^(.*)$ http://mydomainname.com/$1 [L,NC,QSA]
If you want to redirect requests for a subdirectory to the appropriate domain, you can use the rules you have (or similar), but need to specify the scheme http:// and may use the R|redirect flag
RewriteRule ^sub-domain1 http://sub-domain1.mydomainname.com [R,NC,L]
If you also want to forward the requested path, you must capture it and use in the target
RewriteRule ^sub-domain1/(.*)$ http://sub-domain1.mydomainname.com/$1 [R,NC,L]

.htaccess forward from one domain to a specific .html page

I know how to use .htaccess to forward everything in one domain to a new domain name. But in this case, I want everything from one domain to go to a specific .html page on a different domain. That's where I'm lost. I'm trying the following but it just redirects to a folder and the page in question is in that folder but obviously, I don't want people seeing the contents of that folder. Make any sense? So example.com needs to go to yyy.com/some-page.html
This is what I'm currently using:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} (www\.)?5\.xxxx\.com [NC]
RewriteRule ^(.*)$ http://www.1.yyy.com/$1 [R=301,L]
Try:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} (www\.)?5\.xxxx\.com [NC]
RewriteRule .* http://www.1.yyy.com/some-page.html [R,L]
You can also put a blank index.html page to the directory in question to mask its contents or
you can put index.php file with this code <? header ("location: http://www.1.yyy.com/some-page.html"); ?> that will redirect a user to the desired page.
$1 is a place holder for the 1st pattern match. So if you are rewriting domaina.com/someurl/, it is attempting to load domainb.com/someurl/. Swap the $1 with the actual page --- e.g. somepage.html and it should work. But unless both of these domains are pointing to the same files/directories, the rule seems a bit overcomplicated.
So how about just a simple redirect?
Try this in your .htaccess file.
redirect 301 / http://somesite.com/somepage.html
OR you can try this.
RewriteRule ^(.*)$ http://somesite.com/somepage.html [R=301,L]
It does work and you can test my RewriteRule below.
http://htaccess.madewithlove.be/
There must be something else going on.

htaccess redirect if filename equals something

I'd like to set a redirect (preferably with RewriteCond) so that if the requested file is index.php regardless of directory, it will be redirected to another site.
So visiting /index.php or/files/index.php or /stuff/index.php (etc.) will all redirect you to another domain.
Here is a general way to do it. This rule set should be placed in the .htaccess file in the root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} index\.php/? [NC]
RewriteRule .* http://otherdomain.com/ [R=301,L]
Redirects permanently any URL that holds index.php in any position, like
http://mydomain.com/index.php or
http://mydomain.com/any/folder/quantity/index.php or
http://mydomain.com/any/folder/quantity/index.php/any/folder/quantity/
To
http://otherdomain.com/
That's it. You don't explain much so nothing is passed to the other domain, just as you say in your question:
...redirected to another site.
These rules should do it (when placed inside /.htaccess file):
RewriteEngine On
RewriteRule (?:^|/)index\.php$ http://otherdomain.com/ [R=301,L]

Virtual subdomain with wildcard mapping .htaccess

I am building a download site, An users will be able to register and a folder with their username will be created on my server, something like: home/users/username
What I want to accomplish is, if anyone types: username.domain.com in their browser, it will route them to: home/users/username/, and if they type: username.domain.com/file.mp3, it will route them to: home/users/username/file.mp3
If its possible to accomplish sub folders routing, that would be great full aswell, example; home/users/username/sub/file.mp3
Thanks guys
Try adding the following to the .htaccess file in the root directory of your site.
This will rewrite any request to username.domain.com to the correct folder/subfolder and file.
I am assuming that home is a directory in the root folder of your site.
RewriteEngine on
RewriteBase /
#if request for usename.domain.com/anything
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
#send any request that is not already for home/users to home/users/username/anything
RewriteRule ^(?!home/users/) home/users/%1%{REQUEST_URI} [L,NC]
I tried what Ulrich Palha recommended but no luck. I hope this will help guys like me where Ulrich's answer doesn't work.
# Rewrite username.domain.com/<path> to domain.com/home/username/<path>
#
# Rewrite only if not already rewritten to /home/
RewriteCond $1 !home/
# Skip rewrite if subdomain is www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain to %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$
# Rewrite to /home/username/URL-path
RewriteRule (.*) /home/%1/$1 [L]

htaccess command to prevent master site access via subdirectory?

I have hosting setup with a master domain (mapped to the web root) and then a number of addon domains (each with their own folder within the web root). At the moment you can visit www.masterdomain.com/addondomainsubdir and reach the same page as you would if you visited www.addondomain.com (which maps to /public_html/addondomainsubdir). I want to prevent this so if you visit www.masterdomain.com/addondomainsubdir then it will do a 301 redirect to www.addondomain.com. The new addondomain.com site is a single page site so it does not have to map any additional pages.
Adding rules to the htaccess file in the web root does notaffect anything as the subdir exists which is wierd as i thought the htaccess command should work even if there is a matching subdir (i've tried the following which works when there's no matching subdir):
RewriteRule ^addondomainsubdir?$ http://www.addondomain.com [NC,R=301,L]
Logically given it's reaching this directory I figure i need to add a command within the htaccess file in the addondomainsubdir directory however nothing appears to have any effect (i've got various other rules setup and they work fine).
I would be massively grateful if anyone explain the best way to rectify this?
Thanks so much for your help,
Dave
I know this is an old post, but it has never been successfully answered. So for all of you finding this via search, this should do what the OP is asking.
Add this line to your .htaccess file:
redirect permanent /addondomainsubdir/ http://www.addondomain.com
Try these rules in your .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# for http
RewriteCond %{HTTP_HOST} ^(www\.)?masterdomain\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^([^/]+)/?$ http://www.$1.com/ [R=301,L]
# for https
RewriteCond %{HTTP_HOST} ^(www\.)?masterdomain\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^([^/]+)/?$ https://www.$1.com/ [R=301,L]
Instead of putting a rule in your main .htaccess, I would make make a .htaccess for each add-on domain, putting each one in the respective subdirectory.
RewriteEngine on
RewriteCond %{HTTP_HOST} masterdomain\.com$ [NC]
RewriteRule ^addondomainsubdir(.*)$ http://www.addondomain.com/$1 [R=301,L]

Resources