I was wondering if it's possible to create a subdomain using htaccess code only,
(Without setting one in the hosting control panel)
I have a folder named "mobile",
It's location is like so: http://www.domain.com/mobile
I want to be able to enter http://mobile.domain.com and see the mobile folder,
Is this possible to achieve only by writing some code in the htaccess?
if so, how do I do that?
Thanks
You can do this as long as it's configured to handle all requests for *.domain.com domains, then just make sure you have mobile.domain.com's DNS CNAME'ed to www.domain.com.
If you know what subdomains you want, you can enumerate through them like so (putting these rules in the htaccess file in your document root):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.) [NC]
RewriteCond %{HTTP_HOST} ^mobile\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mobile/
RewriteCond %{DOCUMENT_ROOT}/mobile%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/mobile%{REQUEST_URI} -d
RewriteRule ^(.*)$ /mobile/$1 [L]
Otherwise, if you wanted to do this arbitrarily, it gets a little more complicated:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.) [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %1:%{REQUEST_URI} !^([^:]+):/\1/
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /%1/$1 [L]
Depends on the config of your domains.
If you set them up with a wildcard ServerAlias, like so:
ServerName example.com
ServerAlias *.example.com
DocumentRoot /var/www/example.com/
The request will land in the folder and the .htaccess will be evaluated, enabling you to use mod_rewrite. If you don't specify the ServerAlias directive though, the request will never reach the folder and as such you wont be able to do anything with it.
Related
My application webroot is like : /home/username/appname/public_html
I have created a subfolder under public_html and installed the wordpress there.
Like: /home/username/appname/public_html/subfolder
we used the below redirection code because we want to access the application like https://example.com/subfolder
RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://example.com/br/$1 [L,R=301,NC]
RewriteRule ^/?(.*)$ http://example.com/br/$1 [L,R=301]
so when user will access main domain example.com so req will be redirected to example.com/subfolder/
but now we want to access example.com/file.txt
but the problem is due to redirection it also redirecting
so how can I exclude this url example.com/file.txt from redirection to example.com/subfolder/file.txt
Please help us.
I tried to add these config but did not work for me
RewriteCond %{REQUEST_URI} !^/file\.txt$
OR
RewriteCond %{REQUEST_URI} !^example\.com\/file\.txt$
In cPanel (at subdomain management) I've set up a wildcard like to following:
*.example.com > public_html/subDomainHandler
Every (non existent subdomain) request like abc.example.com redirects properly to example.com/subDomainHandler.
But requests like abc.example.com/subfolder doesn't redirect to example.com/subDomainHandler.
I am not at my best in regular expressions, but can anybody give me a RewriteCondition for .htaccess?
Htaccess Rewrite Rule - Try1
I think your answer could be modeled on https://stackoverflow.com/a/8481076/1688441 .
Try the following:
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com\(.*)
RewriteRule ^(.*)$ http://example.com/subDomainHandler/$1 [R=301]
Try2
Another option:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule (.*) http://www.example.com/subDomainHandler%1 [R=301,L]
From: https://webmasters.stackexchange.com/questions/13475/redirect-rewrite-subdomain-to-subfolder
I am in a situation where an user can create his blog in a subdomain.
Users will create blogs and enter the address he wants like say,
abcd.domain.com Then my php code creates a directory called abcd.
To view the blog user will type in abcd.domain.com in his browser and I want a .htaccess code which will rewrite the url and open the files inside the domain.com/abcd
But for the user the url in the browser should stay abcd.domain.com
Currently I am trying this code
RewriteCond %{HTTP_HOST} ^test\.domain\.com$
RewriteCond %{REQUEST_URI} !^test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
But this gives me 404 even though I have a file test.html inside the test folder and trying to view that page.
Also in this situation I will have to manually make change to the .htaccess file for URL rewrite. What I want to know is if it is possible to have a wild card subdomain redirect to the respective directory.
You can use:
RewriteCond %{HTTP_HOST} ^test\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
REQUEST_URI with leading /.
With wild card subdomain:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
Note that it takes more than a rewrite rule to have wildcard subdomains. Just fyi.
You need to have created a wildcard DNS record for subdomains and also tell apache to use any subdomain request by having a ServerAlias of *.domain.com in the apache config.
Then try your rule this way and see if it works for you.
RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$ [NC]
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
I am looking to move http://domain.com/blog to http://blog.domain.com.This also means that everything that trails /blog for example /blog/post/1 will need to be routed to http://blog.domain.com/post/1.
Ensure that you've got content on blog.domain.com. Specifically, if you go to http://blog.domain.com/post/1 you get served the correct content.
In the htaccess file in your domain.com domain's document root, add (preferably above any rules you may already have there):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^/?blog/(.*)$ http://blog.domain.com/$1 [L,R=301]
If you actually don't have any content at blog.domain.com and it shares the same document root as domain.com, then you'll need to add these additional rules:
RewriteCond %{HTTP_HOST} ^blog.domain.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/blog%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/blog%{REQUEST_URI} -d
RewriteRule ^ /blog%{REQUEST_URI} [L]
I have been having a very difficult time editing my .htaccess file to do these three things together. I have been able to get each part separately but I just do not understand how the logic flow works to get them all to work.
This is the best I have been able to pull together using the demo at bluehost support
http://helpdesk.bluehost.com/index.php/kb/article/000347
I appreciate any help resolving this issue.
# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
# Bluehost.com
# .htaccess main domain to subdirectory redirect
# Copy and paste the following code into the .htaccess file
# in the public_html folder of your hosting account
# make the changes to the file according to the instructions.
# Do not change this line.
RewriteEngine on
# Change yourdomain.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?sampleurl.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/folder/
# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /folder/$1
# Change yourdomain.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your folder, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?sampleurl.com$
RewriteRule ^(/)?$ folder/index.php
RewriteCond %{HTTP_HOST} ^sampleurl\.com$ [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://www.sampleurl.com/$1 [L]
# For security reasons, Option all cannot be overridden.
# Options All -Indexes
# Options ExecCGI Includes IncludesNOEXEC SymLinksIfOwnerMatch -Indexes
Update:
Well after a long time using the following I have run into issues.
IndexIgnore *
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule (.*) https://www.example.com/site/$1 [L,R]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/site/
RewriteRule (.*) https://www.example.com/$1 [L,R]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule (.*) https://www.example.com/site/$1 [L,R]
New Issues.
Bluehost has an application that quickly installs and updates open source applications like wordpress, joomla, phpbb etc. Well apparently my new super .htaccess file completely breaks their system. The only way I can run updates and installs is to move the .htaccess to .htaccess.bak until the upgrade or install is complete. Then move it back.
I can't create any .htaccess on other sites that I host in other directories. e.g. I have a master account that I store in /www/site/ that this script forces requests into. Unfortunately my other sites stored in /www/site2/ and /www/site3/ don't work well. First I have to create a blank .htaccess to prevent it from pulling the .htaccess above found in /www/. For some reason if I try to do anything like force www.site2.com I get 500 error's saying I am forcing too many redirects.
I have a theory that my solution above needs to be a little more specific, to make sure it only effects request sent to example.com. (I think the line with anything not containing /site/ is a little to broad).
I appreciate any support resolving this. Would save me a bit of headaches!
Update II:
I do not believe I have access to apache config. Only option I see in cpanel is apache handlers (not really sure what that section does).
I will submit a ticket to see what location the simplescripts server runs from. I believe it runs from "https://www.simplescripts.com". What edit would you recommend I add to ignore this domain?
My host bluehost starts you with a primary account and then subdomains. The primary account's location is set at user\www. Unfortunatly this gets messy when you try to host multiple domains. That is why I use .htaccess to force requests that hit that directory into the \site\ directory. Thus trapping the primary site into the \site\ directory.
I think the relocation issue I was having with one of my subdomains was due to wordpress trying to force a domain without www and then me writing an .htaccess file forcing www. That caused the infinite loop, not my .htaccess file this thread is about. If I can resolve the simplescripts issue I am gmoney ;)
I'd think something like this would work
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*) https://www.example.com/folder/$1 [L,R]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/folder/
RewriteRule (.*) https://www.example.com/$1 [L,R]
but I confess I haven't tested. You can add in all the don't redirect if the file exists, etc., if you want.
In case you haven't seen it, here is the mod_rewrite documentation.
edit:
In response to updated question:
The first thing I notice is that it appears you may have enough control over your setup to put the rewrites in the Apache config files (in the VirtualHost sections) instead of in htaccess files, that'll immediately solve your problem with your other sites.
The first problem, it sounds like you'll need to add RewriteCond to exclude whichever URLs the Bluehost software uses. Either that, or access the Bluehost software under a different VirtualHost (once you've put the rewrite stuff inside the VirtualHost blocks).
If you can't edit your Apache config, then the easiest fix for the other sites it to not make them a subdirectory (on the filesystem) of your main site. That's rather weird, anyway. Move your main site to a sub-directory of /www as well if need be.
Personally, I put all sites in /srv/www/com.site-name.www on my servers (that is, reverse order, with the top-level-domain first, it makes related stuff group together when sorted alphabetically. E.g., when you have www.example.com and static.example.com)
This seems to do everything I needed. Probably a shorter way to get this done but it works!
IndexIgnore *
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*) https://www.example.com/folder/$1 [L,R]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/folder/
RewriteRule (.*) https://www.example.com/$1 [L,R]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*) https://www.example.com/folder/$1 [L,R]