Need help in URL rewriting 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
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]

Related

I need to .htaccess redirect my subfolder to the root folder, except two folders

The subfolder i want to redirect to the root folder is www.example.com/subfolder
So I need to redirect everything in that subfolder to the url adress https://www.example.com except for these two folders (which are placed in the subfolder): www.example.com/subfolder/folder1 and www.example.com/subfolder/folder2
I spent like 4 hours trying to find the exact code but I could not solve that. Nothing worked for me.
I've tried many codes, but nothing worked for me. For example:
RewriteEngine On
RewriteCond %{REQUEST_URI}!^/subfolder/folder1/
RewriteCond %{REQUEST_URI}!^/subfolder/folder2/
RewriteRule ^(.*)$ /$1 [R=301,L]
and
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_URI}!^/subfolder/folder1/
RewriteCond %{REQUEST_URI}!^/subfolder/folder2/
#RewriteRule (.*) http://www.example.com/subfolder/ [R=301,QSA,L]
RewriteRule (.*) https://www.example.com [R]
Could someone help me with that?
I'd prefer to place the .htaccess file in the subfolder.
Thank you so much.
I'd say that your first attempt looks pretty good. Just make a slight modification since you say you want to redirect to the root path, not something inside the root path:
RewriteEngine On
RewriteCond %{REQUEST_URI}!^/subfolder/folder1/
RewriteCond %{REQUEST_URI}!^/subfolder/folder2/
RewriteRule ^(.*)$ / [R=301,QSD,L]
Since you want to use a distributed configuration file (as opposed to the preferred central configuration for the http host) that rule is meant to be used inside a ".htaccess" file inside the "subfolder".
It is a good idea to start out using a R=302 temporary redirection and to only change that to a R=301 permanent redirection once everything works as intended. Also you need to make sure that you always test using a fresh anonymous browser window to prevent client side caching effects.
And you also need to make sure that such distributed configuration files are considered at all by the http server for that location (see the documentation of the AllowOverride directive).

How to rewrite rules for 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
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]

htaccess subdomain rewrite without a redirect

Using htaccess Rewrite, I want my url http://*.phoneataxi.com/ (where * is a wildcard, excluding 'www') to show in the address bar as is but get information from http://*.phoneataxi.com/test.php?c=*.
I have tried so many different things but nothing is doing exactly what I need. Most examples are redirecting the subdomain to the '/test.php' file in the address bar which I don't want to do.
I'm trying not to have to create individial subdomains and subdomain folders within my webroot.
Ideas?
I use this htaccess file to make Apache act as a proxy for another host:
IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ghost\.pileborg\.se$
RewriteRule (.*) http://vps.pileborg.se/ghost/$1 [P]
</IfModule>
It causes all access to http://ghost.pileborg.se/ to be "redirected" to http://vps.pileborg.se/ghost/.
UPDATE (2020)
Some of the answers regarding this topic is very old and no longer work as expected.
After searching for hours on something that actually works, this is what I came up with; edit as you see fit:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ([a-z0-9]+)\.
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.php -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.html -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.html [L,NC,QSA]
Breakdown
Make sure that the rewrite module is installed and enabled on your host
first we turn the rewrite engine on and set the path-base
then isolate the subdomain - any letters/numbers before the first dot
set a variable in this runtime environment that contains the subdomain
check if the subdomain folder and index-file exists
if it does exist -then use that file as the request-handler (no redirect)
if it does not exist then the request carries on normally
Flags
The flags used here are explained here, but the ones used above are quite simple:
[L] Last rule, ignore the rest
[NC] No Case, no uppercase/lowercase restrictions
[QSA] I remember this as "Query String Attach" :D

How to 301 redirect an entire domain while preserving the path

I am redirecting one domain to another, but I want to preserve the path in the redirect. So for example, I want to visit www.example.com/services/education/page.html, but my redirect will bring them to www.new-example.com/services/education/page.html. What do I write in my .htaccess file to preserve the path "/services/education/page.html"?
Right now I have:
redirect 301 http://www.example.com/ http://www.new-example.com/
But I'm not sure if that works or not (Can't test yet as I am waiting for domain details etc). I just want to be sure when I put the site live. Is that right or am I way off base?
Thanks!
This should do it:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ http://new-example.com/$1 [L,R=301]
try adding the following to your .htaccess in the root of your example.com domain
RewriteEngine On
RewriteBase /
#for all requests to www.example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com$
#redirect them to new-example
RewriteRule (.*) http://www.new-example.com/$1 [R=301,L]
Your original command uses the mod_alias Apache module, and it would work, though you may want to update it to:
Redirect 301 / http://www.new-example.com/
Removing the exact domain of the current (old) domain means all domains that point to that folder will be sent to the new domain, making that one-line script more robust.
The other answers use the mod_rewrite Apache module. If you have that also installed, that's fine to use, though it's 4+ lines of code compared to one. Additionally, mod_alias is part of the "base" package, so should be on all Apache servers, while mod_rewrite is an optional extension, so some might not have it.

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