.htaccess RewriteRule subsubdomain.subdomain.domain.tld to subdomain.domain.tld/subsubdomain - .htaccess

Is it posible to redirect a sub-subdomain into a subdomain with the sub-sub domain as new folder using .htaccess rewrite-rule?
For example... When i go to 2013.archive.example.com I want to end up in archive.example.com/2013..
Already tried some things, current .htaccess is:
RewriteRule On
RewriteCond %{HTTP_HOST} ^(.*)\.archive\.example\.com$
RewriteRule ^(.*)$ archive.example.com/%1/$1 [L]
Unfortunately it isn't working. Any suggestions for this?

Changed it a but, currently using:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.archive\.example\.com$ [NC]
RewriteRule ^(.*)$ %1/$1 [L]
and is working exactly how I wanted:)

For external redirect: (URL change in browser)
You can use this rule in your DOCUMENT_ROOT/.htaccess:
RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
This will work provided DOCUMENT_ROOT is same for anything.archive.example.com and archive.example.com.
For internal forward: (No URL change in browser)
You can use this rule in your DOCUMENT_ROOT/.htaccess:
RewriteCond %{HTTP_HOST} ^([^.]+)\.archive\.example\.com$ [NC]
RewriteRule ^ /%1%{REQUEST_URI} [L]

If they share the same root, then you don't need the archive.example.com part in your rule's target:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.archive\.example\.com$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L]

Related

htaccess language rediretion

I have a website, www.thesite.com and an alias www.lesite.com
i need a htaccess redirection:
I would like that, when I go to www.thesite.com/fr, it redirects me to www.lesite.com/fr
Likewise, when I go to www.lesite.com/en, I would like it to redirect me to www.thesite.com/en
I have tried different methods but i only succeed to create infinite loops !----
Options +FollowSymlinks
RewriteRule ^fr$ http://dev.mariage.ch/fr/ [L]
RewriteRule ^de$ http://dev.hortzeit.ch/de/ [L]
OR
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch\/de\/
RewriteRule (.*) http://dev.hortzeit.ch/de$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch\/fr\/
RewriteRule (.*) http://dev.mariage.ch/fr$1 [R=301,L]
You can't use path as part of RewriteCond for HTTP_HOST
instead of dev.hortzeit.ch/de you must use just host part dev.hortzeit.ch
RewriteEngine On
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch [NC]
RewriteRule ^de(/?.*)$ http://dev.hortzeit.ch/de$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch [NC]
RewriteRule ^fr(/?.*)$ http://dev.mariage.ch/fr$1 [R=301,NC,L]

Redirect from subdomain to subdirectory

What I am trying to do is to redirect the user who use for example en.domain.com/blog
to domain.com/blog I have three languages en/es/fr
I tried something like this For the english but its not working.
RewriteCond %{HTTP_HOST} ^en\.domain\.com\/blog
RewriteRule ^(.*)$ /blog/$1 [L]
Anyhelp
Thanks
Put this code in your htaccess (which has to be in root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(en|es|fr)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/blog [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
Put this into your document root folder:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^en\.domain\.com$
RewriteRule ^blog(.*)$ /blog$1 [R,L]
The HTTP_HOST does not contain any URI (path part). Also you have to switch the "rewrite engine" on.

RewriteRule at htaccess wrong redirect

ive this rules at htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^site.com [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [L,R=301]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^warcraft$ www.site.com/forumdisplay.php?f=480 [R=301,NE,NC,L]
issue happend when redirect warcraft its redirect to
http://www.site.com/home/site/public_html/www.site.com/forumdisplay.php?f=480
any tip ?
I suppose you want to redirect it to http://www.site.com/forumdisplay.php?f=480
If so put it in your .htaccess file:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^warcraft$ http://www.site.com/forumdisplay.php?f=480 [R=301,NE,NC,L]
You must provide the full URL including protocol in your rule, in this case http://www.site.com/.....

.htaccess redirect subfolder to HTTPS

I'm trying to write a RewriteRule for my .htaccess file. Specifically, whenever a user accesses a specific subdirectory, I would like it to Rewrite to force an HTTPS connection.
For example, whenever someone accesses: http://www.mydomain.com/subdirectory (and any other sub-directories of that "subdirectory").
I'd like it to rewrite to https://www.mydomain.com/subdirectory
I've tried the following, but it appears to create a loop:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
Also, this .htaccess file is placed in the root of my domain.
Any ideas on how to modify my RewriteRule?
Many Thanks!
I would put this into the domain's root directory:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(subdirectory/.*)$ https://www.mydomain.com/$1 [R=301,L]
This work for me, this allow you to redirect to https a specific folder, just add an htaccess file inside of the folder with the following content:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Ahh, its a combo of the two. MonoMano - you've omitted the subdirectory in the first part of the RewriteRule, therefore directing ALL traffic to the HTTPS subdomain address. I found that checking for 'off' was more reliable than checking for !=on, dont ask me why!
You'd want to add the subdirectory back in as per Floern's response, see below:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(subdirectory/.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
Add the lines given below to .htaccess file of that subdirectory:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/subdirectory/$1 [L,R=301]
Use this if you don't want the address bar to show the subfolder. It will redirect yourdomain.com to yourdomain.com/subfolder but will look like you are still on yourdomain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?yourprimarydomain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
Try This
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
This will remove www prefix , and force https:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://example.com%{REQUEST_URI} [NE,R=301,L]
RewriteRule ^((?!subdirectory/).*)$ /subdirectory/$1 [L]
Change example.com for you main domain and subdirectory for Sub Directory
Try
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]

.htaccess Wildcard Subdomains

I'm trying to implement a solution using .htaccess and wildcard subdomains so that
http://subdomain.example.com is mapped to http://example.com/index.php/accounts/subdomain/. My rules look something like:
RewriteCond %{HTTP_HOST} !www.example.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).example.com [NC]
RewriteRule ^(.*/) /index.php [PT,L]
Which works, but disregards everything else. When I try appending anything to the rule e.g:
RewriteRule ^(.*/) /index.php/hello [PT,L]
I get a 500 internal server error. How do I get this working?
You probably need to exclude the index.php from your rule:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]
This is an adaptation of the code I use to redirect subdomains on my own site. I make no claims to it being best practice but it works;
RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %1 !^(www)\.example$ [NC]
RewriteRule ^.*$ http://www.example.com/index.php/accounts/%1/ [R=301,L]
Try changing your RewriteRule to
RewriteRule ^/(.*)$ /index.php/accounts/%1/$1 [PT]
That will rewrite the URL to one that includes the subdomain and the original request URI.
EDIT: maybe it needs to be
RewriteRule ^(.*)$ /index.php/accounts/%1/$1 [PT]
as mentioned in the comments.

Resources