Using Subdirectory as subdomain in htaccess - .htaccess

I am trying to point a subfolder on my site as a subdomain. for example, i want to turn www.domain.com/test to test.domain.com. I already have some rewrites in htaccess done by developers that we used earlier in the site building process, and I'd like to be able to add this line without interfering with those.
I've tried dozens of answers to this issue i found across the net, but none of them seemed to work and i'm not sure if it's due to my lack of knowledge of this language or if there's a conflict with something else.
The latest lines I tried are:
RewriteCond %{HTTP_HOST} !^www.domain.tv
RewriteCond %{HTTP_HOST} ([^.]+).domain.tv
RewriteRule ^(.*)$ /var/www/www.domain.tv/test%1
This is my current htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#RewriteCond %{HTTP_HOST} !www\.domain\.tv$ [NC]
#RewriteCond %{HTTP_HOST} ^(.+)\.domain\.tv$ [NC]
#RewriteRule .* index.php?option=com_contushdvideoshare&view=player&Itemid=33 [L]
RewriteCond %{REQUEST_URI} ^/album\/t\/([a-zA-Z0-9]+) [NC]
RewriteRule ^album/t/(.*) index.php?option=com_usermenu&task=directdownload&id=$1&type=one$
RewriteCond %{REQUEST_URI} ^/album\/([a-zA-Z0-9]+) [NC]
RewriteRule ^album/(.*) index.php?option=com_usermenu&task=directdownload&id=$1 [L]
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} (/[^.]*|\.(php|html?|feed|pdf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Can anyone help me here?

Try:
RewriteCond %{HTTP_HOST} !^www.domain.tv [NC]
RewriteCond %{HTTP_HOST} ([^\.]+).domain.tv [NC]
RewriteRule ^(.*)$ %1/$1 [L]

Subdomains aren't so easy to set up. You will need to configure your server before. More informations here : Create subdomains on the fly with .htaccess (PHP)

Related

htaccess mod-rewrite language overrides

I am using some mod-rewrite code in my sites .htaccess file to redirect users with browser languages set to various languages to different versions of my site. This is working well - but... I need a way to be able to override these rewrite rules when a user wants to view the main English version instead of the version they would get automatically redirected to based on the language setting of their browser.
So for example, user A has their browser language set to Korean, they visit the example.com site - the rewrite rules reads they are set to Korean and moves them to example.co.kr - on example.co.kr I have a button which says - Show me the English version of this website (which links to example.com) - this then links back to example.com... but currently then redirects back to example.co.kr as the rewrite script refires...
How can I code the rewrite script to allow for an overwrite rule to force it to stay on the example.com site when I want it too...?
rewrite code I have is:
RewriteEngine On
RewriteBase /
#RewriteCond %{HTTP:Accept-Language} ^en [NC]
#RewriteRule ^$ https://example.com/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^ko [NC]
RewriteRule ^$ https://example.co.kr/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^pt-PT [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^pt-BR [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(|ko|pt|pt-PT|pt-BR)/?$ https://example.com/ [QSA,NC,L]
Any ideas?
Cheers,
You need to modify your rules to look for a special query parameter e.g. notKO like this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ https://example.com/ [L,R=301]
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^ko [NC]
RewriteRule ^$ https://example.co.kr/ [L,R=301]
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^pt-PT [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{QUERY_STRING} !notKO [NC]
RewriteCond %{HTTP:Accept-Language} ^pt-BR [NC]
RewriteRule ^$ https://example.com/br/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(|ko|pt|pt-PT|pt-BR)/?$ / [NC,L]
Now you need to start sending ?notKO query parameter on your show me abc version of this website button click.
Clear your browser before you test this change.

Joomla .htaccess

I need a working RewriteRule to get rid of the /index.php/category/ part of my url(s).
bruteforce.tv/index.php/streams/shinigamily
bruteforce.tv/index.php/streams/cash
bruteforce.tv/index.php/streams/blacktigrex
bruteforce.tv/index.php/streams/viktorwwe
TO
bruteforce.tv/shinigamily
bruteforce.tv/cash
bruteforce.tv/blacktigrex
bruteforce.tv/viktorwwe
I'm not used to it with working with a htaccess file so I apologize for being completely unresourceful.
This is my current htaccess file with the not working RewriteRule ^streams/(.*)/?$ /$1 [L,NC,R]
Options +FollowSymLinks
## Mod_rewrite in use.
RewriteEngine On
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^streams/(.*)/?$ /$1 [L,NC,R]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Use Joomla's built-in feature to remove /index.php (under global config > site)
RewriteRule ^streams/(.*)$ http://bruteforce.tv/$1 [R=301,L]

Excluding specific pages from HTTPS in mod_rewrite

I am trying to setup a codeigniter app to force HTTPS across all pages except one. However, I cannot get the rules to only redirect if the user is not on the page in question.
The page that should be excluded has the following URL's:
http://mydomain.com/kpi/reports/67
http://mydomain.com/kpi/reports/67/overview/2013-02-01/2013-02-28
http://mydomain.com/index.php?/kpi/reports/67
http://mydomain.com/index.php?/kpi/reports/67/overview/2013-02-01/2013-02-28
The number 67 and the dates can all change in the URL's above hence the user of regular expressions below.
I have tested the regular expressions and they seem to match the URL's fine. However, the htaccess just seems to redirect it to https:// anyway.
My .htaccess file is as follows...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Disallow access to system dir
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Disallow access to application dir
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Force https when not on overview report
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/index\.php\?/kpi/reports/?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/index\.php\?/kpi/reports/?([0-9]+)/overview/?([0-9]+)-?([0-9]+)-?([0-9]+)/?([0-9]+)-?([0-9]+)-?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/kpi/reports/?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/kpi/reports/?([0-9]+)/overview/?([0-9]+)-?([0-9]+)-?([0-9]+)/?([0-9]+)-?([0-9]+)-?([0-9]+)$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L]
#If not a valid file, redirect request through index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Any help would be greatly appreciated!
Maybe this will do what you need:
#Force https when not on overview report
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !kpi/reports/[0-9]+/?$ [NC]
RewriteCond %{QUERY_STRING} !kpi/reports/[0-9]+/overview/[^/]+/[^/]+/? [NC]
RewriteCond %{REQUEST_URI} !kpi/reports/[0-9]+/?$ [NC]
RewriteCond %{REQUEST_URI} !kpi/reports/[0-9]+/overview/[^/]+/[^/]+/? [NC]
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L]
#If not a valid file, redirect request through index.php
Replace all lines between the comments.

joomla htaccess RewriteRule issue

i have a redirect in my htaccess which works perfectly in joomla before i swicth on SEF URLS
RewriteRule ^m/(.*) /index.php?option=com_seemail&view=mail&guid=$1&itemid=102
however when i switch on SEF URLS then it no longer works - im directed to /home/uncategorised/welcome instead
this is my htaccess :
RewriteEngine On
RewriteRule ^m/(.*) /index.php?option=com_seemail&view=mail&guid=$1&itemid=102
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^apply$ /index.php?option=com_loans&view=apply&Itemid=102 [R=301,L,QSA]
RewriteBase /
DirectoryIndex index.php
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
can anyone help please ?
RewriteRule ^apply$ /index.php?option=com_loans&view=apply&Itemid=102 [R=301,L,QSA]
based on the above line of code, if there is nothing after "apply" in the url, it should redirect to /index.php?option=com_loans&view=apply&Itemid=102 and stop looking at other rules for the said case.
As you said it is redirecting that means it is working.
Can you manually open the <yourdomain.com>/index.php?option=com_loans&view=apply&Itemid=102
If it opens and the url is sef, copy the sef url after the domain and replace /index.php?option=com_loans&view=apply&Itemid=102 with that in the RewriteRule.
May be that should work.

https in htaccess and order of rules (using Expression Engine)

I'm building a site in expression engine that part of needs to be https. The site is also using a new domain name (new one www.example-new.com the old one www.example.old.com).
I want to do the following things:
remove the index.php
force www
force https for any url starting www.example.old.com/extranet
redirect https URLs that are not www.example.old.com/extranet (e.g. www.example.old.com/news to http
I have the following code so far that works for the first two requirements:
<IfModule mod_rewrite.c>
RewriteEngine On
# Force www
RewriteCond %{HTTP_HOST} ^example-new.com$ [NC]
RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L]
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
AddType x-httpd-php53 .php
I seem to be going round in circles, so I've two questions that will help me write the other rewrites (although feel free to share suggestions...):
1) Should the code for requirements 3 and 4 be positioned before "removes index.php" code?
2) Does the position have any bearing on the redirects that will be coming from the old site e.g. www.example-old.com/some-link-here.asp will be redirected to www.example-new.com/some-new-link-here
Thanks,
Gregor
1) Remove 'index.php' from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
2) Add 'www' to all URIs
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
3) Force https:// for any URI starting with /extranet
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/extranet(.*)$ [NC]
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
4) Redirect https:// URIs that are not /extranet
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/extranet(.*)$ [NC]
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
Putting it all together, here's your complete set of RewriteRules:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/extranet(.*)$ [NC]
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/extranet(.*)$ [NC]
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
1) Should the code for requirements 3 and 4 be positioned before "removes index.php" code?
The rules are processed in the order that you write them. I expect that you want the site to redirect first then do the remove index, so yes, it should go before
2) Does the position have any bearing on the redirects that will be coming from the old site e.g. www.example-old.com/some-link-here.asp will be redirected to www.example-new.com/some-new-link-here
If you are using the same directory for both sites, then you would need to prefix all rules for one site with a RewriteCond that limits the domain. Otherwise, order of the rules (old vs new site) is important.
Your redirects from the old site should also incorporate rules in the new e.g. ensure that you are going to http/s as necessary to avoid extra redirects.
Below is the code to force http/s
#if not secure
RewriteCond %{HTTPS} off
#and starts with /extranet
RewriteCond %{REQUEST_URI} ^/extranet [NC]
#redirect to secure
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#if secure
RewriteCond %{HTTPS} on
#and does not start with /extranet
RewriteCond %{REQUEST_URI} !^/extranet [NC]
#redirect to http
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force www on hostname, but keep same protocol (http/https)
RewriteCond %{HTTP_HOST} !^www\.(.+)
RewriteCond %{HTTPS}s ^(on(s)|offs)
RewriteRule ^ http%2://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If it helps... I just did this yesterday for an ecommerce site... Here's my .htaccess file
# Remove WWW from URL
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteCond %{REQUEST_METHOD} !=POST
RewriteRule ^(.*)$ $1/ [L,R=301]
#remove index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
#Force HTTPS on checkout/account pages
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (checkout|account)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#remove HTTPS on all other pages
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(img|_|images|checkout|account)
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Resources