Htaccess redirect without url rewrite - .htaccess

I have a little problem in hands.
I am setting up a domain that as 3 languages, example.com for principal domain, en.(...) for english and de.(...) for germany.
Usually I only redirect the httpdocs from the subdomains to the main with ln -S because all languages executes the same code, the difference is matched from php and mysql.
My new host don't provide any ssh connection so I have to use a different approach on this.
It was told to me that it can be done by .htaccess but I already tried a lot of things but only can redirect, changing the url and that's not possible, it have to keep the same, the contents yes, will be from another domain. Can someone help?

This code should looks like the one you're searching for :
www.domain.en .htaccess
RewriteBase /
RewriteRule ^(.*)$ http://www.domain.com/en/$1 [L,R=301]
www.domain.de .htaccess
RewriteBase /
RewriteRule ^(.*)$ http://www.domain.com/de/$1 [L,R=301]
You'll propably adapt the http://www.domain.com/lang/$1 part to your needs.
EDIT
Following your comment, this is a code for subdomains redirections :
domain.com .htaccess
RewriteCond %{HTTP_HOST} ^en\.domain\.com [NC]
RewriteRule (.*) http://domain.com/en/$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^de\.domain\.com [NC]
RewriteRule (.*) http://domain.com/de/$1 [QSA,L]

Related

Redirection on Litespeed server not working as expected

Litespeed obeys .htaccess rules but I cannot seem to get it to work for what should be a basic problem for which I cannot find a solution online. I want to redirect from this with everything in the sub-directory:
https://example.com/mail/
to
https://mail.example.com/
What I have tried so far that does NOT work:
SETUP 1
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^/mail$
RewriteRule ^(.*)$ https://mail.example.com/$1 [R=301,L]
SETUP 2
RewriteRule ^mail/(.*)$ https://mail.example.com/$1 [R=301,NC,L]
SETUP 3
Redirect 301 /mail/ https://mail.example.com/
I have also tried another 5 things that don't work. The mail.example.com server is different from the one example.com/mail/ is sitting on.
EDIT TO ANSWER COMMENTS:
I have put the .htaccess file both in the root directory and the mail sub-directory but it does not redirect. By not working, I mean that the page just goes back to the original URL (example.com/mail/) instead of redirecting as expected.
Your setup #1 is almost OK. The reason why it failed is because the RewriteCond %{HTTP_HOST} ^/mail$ is not matched. You need to change the pattern ^/mail$ to (www\.)?example\.com$ where example.com is your main domain and place the code in /mail/.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^(.*)/?$ https://mail.example.com/$1 [R=301,L]

Domain redirection with folder separation. htaccess

After redirecting my client domain to my server i created rewrite rule in root folder .htaccess file to point domain to a subfolder1 (joomla website):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myclientdomain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/subfolder/
RewriteRule ^(.*)$ /subfolder1/$1 [L]
Everything worked fine but today i found out that i can access any other subfolder in my server by entering for example: myclientdomain.com/subfolder2 and what's worse google can index that and show it in search results.
If there is any way to redirect a domain in a way that I won't be able to access any other folder on my server?
I would really appreciate help as I searched throughout google for answer, my server tech support said that they don't really support these kind of problems (they only gave me a piece of code from above) and I don't really know anything about .htaccess rules and how it works.
Try this
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myclientdomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder\/?(.*)?$
RewriteRule ^(.*)$ /subfolder1/$1 [L]
Change above rule to:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myclientdomain\.com$ [NC]
# if current URI is not starting with /subfolder/ then route to /subfolder/
RewriteRule ^((?!subfolder1/).*)$ subfolder1/$1 [L,NC]

How do I create a subdomain and then have it forward to a subfolder?

Say I have the site blog.mydomain.com and www.mydomain.com. I want to make sure that if people type in blog.mydomain.com, that it'll redirect to www.mydomain.com/blog.
(I know the question is usually asked the other way around..)
I found and edited the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog.mydomain.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/blog/$1 [R=301,L]
Is this correct?
And: Do I need to create a record in my DNS to make the subdomain? (since this .htaccess stuff is on the regular domain of course and not on the blog.mydomain.com..)
This should do the trick
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com
RewriteRule ^(.*)$ http://mydomain.com/blog/$1 [R=301]

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]

modrewrite - rewrite to new domain from subdirectory

Damn you modrewrite
I have a website hosted at a url like:
http://mydomain/mocks/thesite/
Now I want to move it to a new domain
http://thesitesdomain.com/
My htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule (.*) http://www.thesitesdomain.com/$1 [R=301,L]
Now this works fine as long as there is something after /mocks/thesite/. eg: http://mydomain/mocks/thesite/index.html redirects to http://www.thesitesdomain.com/index.php.
However the problem is that:
http://mydomain/mocks/thesite/ redirects to http://thesitesdomain.com/mocks/thesite/. Any idea why? How to stop this?
The .htaccess file is in the root of /mocks/thesite/ (if that helps)
Thank you
You should try to use the variable REQUEST_URI you might have a little more success with that. It should be the request uri and file name. To
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule .* http://www.thesitesdomain.com/%{REQUEST_URI} [R=301,L]
I can't remember but to also redirect with the query string (get variables) I think you need to add it like this.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule .* http://www.thesitesdomain.com/%{REQUEST_URI}?%{QUERY_STRING} [R=301,L]
Been a while since really doing a domain redirect....
BTW this is a good read on htacces configuration:
http://corz.org/serv/tricks/htaccess2.php

Resources