Folder to subdomain - .htaccess

My server provider stopped give me possible way to create subdomains in control panel and they say I should solve it with htaccess.
So I have to do something like this. When user types http://asdf.example.com it should get the content of http://example.com/asdf (as the provider changed even the FTP's structure... how kind of them)
I don't find any tutorial for this. Thanks in advance.

More generic version of TerryE's answer. I haven't tested it though!!!
RewriteCond %{HTTP_HOST} !www.example.com
RewriteCond %{HTTP_HOST} (.*).example.com
RewriteCond %1/$0 !^([^/]+)/\1
RewriteRule ^.* %1/$0 [L]

You can do that with mod_rewrite and mod_proxy:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^asdf\.example\.com$
RewriteRule ^ http://example.com/asdf%{REQUEST_URI} [L,P]

You don't need mod proxy. Just do an internal redirect:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} =asdf.example.com
RewriteCond $0 !^asdf/
RewriteRule ^.* asdf/$0 [L]
The rule rewrite the request prefixing asdf/ but only if (1) the host is http:/asdf... and (2) the rewrite hasn't already taken place. You need (2) to prevent iterative loops. $0 is the match string in the rule.
Hope this helps.

Related

Root redirects to just one subdomain in .htaccess

I want the root of my website (www.bitumephotofest.it) to redirect to a subdomain (2016.bitumephotofest.it) (I am running a Wordpress multisite). It works.
But I have another subdomain (2015.bitumephotofest.it) and it also redirects to 2016.bitumephotofest.it.
I want the redirect to work only between www.bitumephotofest.it and 2016.bitumephotofest.it. 2015.bitumephotofest.it should be independent as it is a different website.
I tried to look for questions by people with a similar situation but there is always something different and, anyway, those solutions does not work for me.
Here is my code:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} 2016.bitumephotofest.it
RewriteCond %{REQUEST_URI} !wordpress/
RewriteRule ^(.*)$ /wordpress/$1 [L]
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteRule !^(2016) http://2016.bitumephotofest.it [L,R]
Does anyone know what I am missing?
Thank you in advance!
If you want the domain 2015.bitumephotofest.it stay unmodified, you can sort of exit the rewrite rule chain with
RewriteCond %{HTTP_HOST} ^2015\.bitumephotofest\.it$
RewriteRule ^ - [L]
- as the target means don't rewrite, see RewriteRule
- (dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.

mod_rewrite causes 500 server error/Fails/works partially

I am trying to do a permanent redirect from my old design (www.your-translations.com) to my new design at www.your-translations.com/_YT.
In the process, the urls have to change from this format:
www.your-translations.com/my_page.php
to this format:
www.your-translations.com/_YT/index.php?pge=my_page
I have made many attempts so far, the latest of which is:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*your\-translations.com.*$ [NC]
RewriteRule ^(.*?)/([^/]*)\.php $1/_YT/index.php?pge=$2 [R=302]
RewriteCond %{HTTP_HOST} ^your-translations.com [NC]
RewriteRule ^(.*)$ http://www.your-translations.com/$1 [R=302,L]
This is one of my better tries in that it apparently does almost nothing (previous tries have resulted in 505 errors and infinite loops). The rewriting of URLs to "www." works properly.
I have tested the regex in different regex testers and it seems to do what I expect it to do.
Can someone explain what I am doing wrong?
WHAT exactly is the pattern applied to? Is it what is matched by the RewriteCond? The URI as I typed it in? The relative path? The actual path on the server?
Is there any way to display the string before I try to match it?
Several suggestions found in tutorials result in infinite loops on my server. Do all versions work the same way?
Unfortunately, the webhost technical support doesn't seem to know anything about mod_rewrite, so I can't expect any help from them.
According to my FTP log, I have already made about 80 attempts and I could really use a hand here.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-translations\.com$
RewriteCond %{REQUEST_URI} !^/_YT/([a-z0-9-_]+)\.php$
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)\.php$
RewriteRule ^(.*) /_YT/index.php?pge=%1 [QSA]
RewriteCond %{HTTP_HOST} ^www\.your-translations\.com$
RewriteCond %{REQUEST_URI} !^/_YT/([a-z0-9-_]+)\.php$
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)\.php$
RewriteRule ^(.*) /_YT/index.php?pge=%1 [QSA]
In RewriteCond, you must type a backslash before a dot and not before dash, because a dot in .htaccess means any character, so you need bslash to call is as dot. Sorry, I'm in mobile, so I couldn't explain more, but feel free to ask what you want to know about the code.
Thanks to some explanations from webmasterworld, I was able to fix my mod_rewrite code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-translations.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.your-translations.com [NC]
RewriteCond %{REQUEST_URI} !="^.*?/.*"
RewriteRule ^([^/]*)\.php$ http://www.your-translations.com/_YT/index.php?pge=$1 [R=302,L]
RewriteRule ^$ http://www.your-translations.com/_YT/ [R=302,L]
This seems to work correctly so far (touching wood). Thanks Servant for your help.

Redirecting Subdirectory to Subdomain with htaccess

I'm relatively new to using .htaccess, and have never done any coding besides what I've read online. I'm using Bluehost, and I'd like to redirect my blog subdirectory to a subdomain. Example: I'd like to redirect www.example.com/blog to blog.example.com.
I already have code in place to always add www. to the beginning of my blog address in the root folder, but I don't know how to accomplish the above redirect by using code in .htaccess. Any help would be appreciated!
A lot of web hosts today provide an easy implemention for subdomain creation in their administration panels. You just need to to go there, choose you subdomain name, and then point it to a directory in your tree.
If you can't, then it will be a little more complicated (You will need to resolve that subdomain to your server ip, configure some virtual hosts ... etc) and you may not have enough privileges to do that (unless you are on a dedicated server).
Edit 2
To redirect requests to www.example.com/blog to blog.example.com, try this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteCond %{REQUEST_URI} !^blog/
RewriteRule ^(.*)$ /blog/$1 [L,QSA]
I wanted to add my two cents,
1) to answer the question above, this rewrite should fix it:
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^/blog$ http://blog.example.com [R=302,L]
2) but, I think this is not enough by itself, you also need to change DNS, so that blog.example.com is pointed at the right server, and this can be done by a cname similar to this:
blog.example.com CNAME example.com TTL 1080
(not exactly how it will look, but use your DNS webinterface to set this up).
Have you tried this one?
RewriteEngine on
RewriteBase /
RewriteRule ^/blog/(.*)$ http://blog.subdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^check.domain.info$
RewriteCond %{REQUEST_URI} !^/check/
RewriteRule (.*) /check/$1
To redirect subdomain1 and subdomain2 and directory3 to a directory with HTTPS://, I use the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain1.example.com [OR]
RewriteCond %{HTTP_HOST} ^subdomain2.example.com [OR]
RewriteCond %{HTTP_HOST} ^example\.com/subdomain3 [NC]
RewriteRule ^(.*)$ https://example.com/subdirectory/$1 [R=301,L]

RewriteRule [L] still changing the url

I found myself with this problem, which is driving me a little bit crazy. I use apache's mod_rewrite for pretty URLs and I need to use dynamic subdomains in the site. Everything is great and all the server has de wildcards. I use the next code on my .htacess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.com
RewriteCond %{HTTP_HOST} ([^.]+).mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/%1 [L]
The only problem is, even if I use the [L] flag the url of the site change to http://mysite.com/subdomain. What i want is the url to be like http://subdomain.mysite.com
The link mysite.com/subdomain is a dynamic url and is solved with another rule with the following code:
RewriteRule ^([A-Za-z]+)$ filter.php?type=subdomain&subdomain=$1
Any help would be appreciated
If you specify an external URL (which changing the subdomain does), a header redirect will take place. I don't think you can prevent that. But why not skip that step altogether, and use the second RewriteRule straight away?
I can't test this right now, but something like
RewriteCond %{HTTP_HOST} !^www.mysite.com
RewriteCond %{HTTP_HOST} ([^.]+).mysite.com [NC]
RewriteRule ^(.*)$ filter.php?type=subdomain&subdomain=$1
should work.

Htaccess Redirect

I am looking to make www.purchase.example.com redirect to purchase.example.com, below is an example of what I am trying to do:
RewriteCond %{HTTP_HOST} ^www\.purchase\.
RewriteRule (.*) http://purchase.DOMAIN_NAME/$1 [L,R]
I need a variable that will replace DOMAIN_NAME with simply purchase.example.com.
Obviously I can hard code the purchase.example.com but I will need the code to work on multiple sites. Any suggestions?
For your knowledge:
I used a RewriteCond backreference:
RewriteCond %{HTTP_HOST} ^www\.purchase\.(.*)
RewriteRule (.*) http://purchase.%1/$1 [L,R]
I would not do this in code, I would do this on the web hosting account.
If you need it a little more generic redirect for every domain starting with www.:
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule ^ http://%0%{REQUEST_URI} [L,R=301]

Resources