How can I convert a folder into a sub domain? - .htaccess

I have a website that looks like this (the Ferrari is not a physical folder is an alias virtual):
www.example.com/en/ferrari
and I would like to convert this site into a sub-domain, like so:
ferrari.example.com
or something like:
ferrari.example.com/en/
The idea is to have all sub-pages from the Ferrari section working inside the sub-domain.
I don't know if this is even possible, all my .htaccess test didn't worked out.
I am searching for a re-write rule but I don't seem to find it.
EDIT:
Let me rephrase the question, I don't want to REDIRECT from example.com/ferrari to ferrari.example.com
What I want is to show the example.com/ferrari site when someone opens ferrari.example.com. That is the difficult part. If someone opens ford.example.com, I want to show all the pages that are inside example.com/ford.

You can use that in your root .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ferrari\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/en/ferraricars [NC]
RewriteRule ^(?:en/)?(.*) en/ferraricars/$1 [NC,L]
Work with ferrari.example.com/en/other and ferrari.example.com/other -> example.com/en/ferraricars/other

I had faved this link a year ago and it worked for me.
PS: I don't remember, and I'm not sure, but I think that I have added some other flags in the htaccess. Let me know if it not working on comments.

With directadmin it would be possible with creating a subdomain + hatcc redirect

Related

Internal rewrite to remove directory from url with htaccess

I've searched and tried many tutorials and problem solutions but couldn't achieve what I want.
If user open domain.com/xxx.php he see the content of domain.com/hosting/xxx.php
This
RewriteRule ^xxx.php$ /hosting/xxx.php
Works perfectly what i want to do, but there are over 150 pages in hosting directory and writing rules for each page does not seem right? any possible way to do it dynamically?
I tried this solution it works but it try to find those pages which are currently at root domain.com/xxx.php inside domain.com/hosting/
I search slimier questions have been asked many times before but non of the answer worked for me.
You need to also take care of reuests to the shortened, rewritten URL:
RewriteEngine on
RewriteRule ^/?hosting/(.*)$ /$1 [END,NC,R=302]
RewriteRule ^/?(.*)$ /hosting/$1 [END]

.htacces redirect from domain 1 to domain 2 subdir, keeping path

I have found plenty of useful answers in this forum that got me to the point I'm at, however the configuration of my .htaccess redirect is still not 100% perfect.
Set-up
I have two domains registered:
domain1.is (only domain)
domain2.me (domain + hosting)
The domain1 redirects as wildcard everything to domain2.
What I'm trying to do
I have installed yourls (url shortened) on domain2.me/yourls and want to redirect all domain1.is/randomstring to domain2.me/yourls/randomstring.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1\.is$
RewriteRule (.*) https://domain2.me/yourls/$1 [R=301,L]
RewriteRule ^$ store [L,R=301]
The issue
So far I got that domain1.is/randomstring goes to domain2.me/randomstring, however the subdirectory in the .htacess file seems to be completely ignored, effectively rendering the whole system unusable.
I've tried adding a condition, changing the subdirectory, etc. but can't seem to find the reason why. Can anyone help please?
Thanks in advance and cheers from Berlin!
I'll share my solution in case someone else stumbles upon this conversation. The details/history are in the comments of the question.
TLDR;
Just set up a wildcard redirect to the subdirectory.
(not sure if the first redirect is useful, but still).
Important point: you don't even need to touch the .htaccess file (in my particular case yourls already had one, so I left it as it is).
Credit: thanks #CBroe for pointing me in the right direction.

Short URL system: How to redirect the Custom URLs?

I'm trying to make a tinyurl-like service for my company, and so far looks good, but now I have a problem I can't solve.
Lets say the URL I generate is "www.thecompanyiworkfor.com/shorturl/2jh62/". My guess is I have to use some script, lets say "redirect.php", where I access the databank, look for that short url, find the original one, and redirect with headers.
My question is, how can i make that "www.thecompanyiworkfor.com/shorturl/2jh62/" open "redirect.php" and that I can access the "shorturl" as a parameter? I thought I would have to do something with .htaccess, but I'm not really sure what should I do...
Help please!
Here's what I recommend.
1) Create a sub domain (s.thecompanyiworkfor.com). It will be easier to manager and you avoid conflict with .htaccess since this folder is separated from the main WWW folder.
for example:
s.thecompanyiworkfor.com => /home/thecompanyiworkfor.com/s_public_html/
www.thecompanyiworkfor.com => /home/thecompanyiworkfor.com/public_html/
2) Use this .htaccess in the /home/thecompanyiworkfor.com/s_public_html/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^index\.php
RewriteRule ^([a-z0-9\-]+)(\/?)$ index.php?code=$1 [L,NC,QSA]
Then in your /home/thecompanyiworkfor.com/s_public_html/index.php you can check which code correspond to which URL and redirect. If not found, redirect to www.thecompanyiworkfor.com

.htaccess Rewrite Based on Existence of Path in URL

Here's the scenario, I have a website that used to be a static HTML site and WordPress blog using a subdomain (http://blog.domain.com).
I recently combined everything into a single WordPress installation. To maintain old links I had to rewrite requests like "http://blog.domain.com/index.php/2010/10/16/post-name" to "http://domain.com/index.php/2010/10/16/post-name". My problem is that when trying to visit just "http://blog.domain.com", I get redirected to "http://domain.com" when I want it to go to "http://domain.com/index.php/blog".
So, if a user requests "http://blog.domain.com" (by itself, with or without slash), I want it to go to "http://domain.com/index.php/blog". If they request an old URL of "http://blog.domain.com/some-link-to-a-post", I want it to redirect to "http://domain.com/some-link-to-a-post". In other words, if it's a URL to an actual post, I just want to strip the "blog" subdomain. If it's the old link to the main blog page, I want to remove the "blog" subdomain and append "/index.php/blog"
http://blog.domain.com/ -> http://domain.com/index.php/blog
http://blog.domain.com/index.php/2010/10/16/post-title -> http://domain.com/index.php/2010/10/16/post-title
Hopefully that's clear. I'm not an htaccess expert, so hopefully someone can help me out here. Thanks in advance!
Using the [L] command at the end of a rewrite will tell htaccess that this is the last rule it should match. If you put a rule to match your first condition at the top and the other rewrite rule you said you had already created after it, you should get your expected result.
Try this:
RewriteRule ^blog.domain.com(/?)$ domain.com/index.php/blog [L]
# Your other rewrite here #
I couldn't get that solution to work. However, I used the following:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/index.php/blog/$1 [R=301,L]
That ends up in a URL like http://domain.com/index.php/blog/index.php/2010/06/04/post-title, but Wordpress is smart enough to fix it.

htaccess redirect from root to subfolder and then mask the url?

Two things:
Firstly - I have version 2 of a website located in a folder named v2, and I want to redirect any traffic that is NOT a child of the v2 folder, to www.example.com/v2
The old site located in the root was created in iWeb and has a LOT of subfolders and sub-subfolders.
So:
www.example.com/v2 = New site
www.example.com/Page.html
www.example.com/category/Page.html
ww.example.com/category/subcategory/Page.html = All generic examples of what I need to redirect.
Secondly, and I don't know if this is possible, I want to hide /v2/ in the URL, so that visitors will just see www.example.com/page even though they are actually on www.example.com/v2/page
Links are hardcoded to the v2 folder, like so <a href="v2/contact.html">
Any help is MOST appreciated. I've spent hours trying to figure this out, but I'm only just learning about htaccess and regular expressions, and am totally confused.
Thanks so much!
RewriteCond %{REQUEST_URI} !^v2/
RewriteRule ^(.*)$ v2/$1 [L]
rewrite everything by including v2 before it .
RewriteRule ^(.*)$ v2/$1 [L]
www.example.com/Page.html should now be processed as /v2/Page.html
You should not include v2/ in the url being sent to the user.
it would then become v2/v2/

Resources