.htaccess Redirect main domain to subfolder - apache

I am using one account for multi sites and I am wanting to have a clean public_html dir so I am trying to create my .htaccess to redirect the main domain to the subfolder.
I currently have as my result http://domain.co.nz/subfolder.
How can I work it so it just shows the original domain and also for www/http?
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.co.nz$
RewriteRule ^(/)?$ subfolder [L]

You need to adjust your code a little.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.nz$
RewriteCond %{REQUEST_URI} !^/subfolder [NC]
RewriteRule ^(.*)$ /subfolder/$1 [L]

Related

htaccess redirect with question mark

Hello I'm trying to redirect /fr/welterweight.php which is a local folder on my website via apache conf or .htaccess.
To http://www.bluek.com/fr/welterweight.php?c=france which is another website I own.
I tried
RewriteCond %{REQUEST_URI} /fr/welterweight.php
RewriteCond %{QUERY_STRING} ^welterweight.php$
RewriteRule ^/?$ http://www.bluek.com/fr/welterweight.php?c=france
But its not working
This rule should work from site root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^fr/welterweight\.php$ http://www.bluek.com/$0?c=france [L,R=302]

using htaccess in subfolders to redirects

i'm running a site with 2 subfolders (different sites in each i.e. I have 3 different sites in one host) so my question is that,
can I use .htaccess file in the root of subfolders? will it work? or I have to do all from my main site root and .htaccess?
would you please suggest a common code for my sobfolders to redirect to www versions with .ir prefix and remove index.php?
i want this:
mysite.ir/news [or] mysite.com/news [or]
to
www.mysite.ir/news
and also to remove index.php:
mysite.ir/news/index.php?/title [or] mysite.com/news/index.php?/title
to
www.mysite.ir/news/title
Thank you guys, any would be much appreciated
In the root folder of mysite.com. Create a .htaccess file with the below rules
RewriteEngine On
RewriteRule ^(.*)$ http://www.mysite.ir/$1 [R=301,L]
In the root folder of mysite.ir. Create a .htaccess file with the below rules
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.mysite.ir/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)/$ /$1/index.php?/$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)$ /$1/index.php?/$2 [L]

.htaccess conflictions on different directories

Probably a duplicate question but here goes!
I'm not great at htaccess stuff. I've been using codeigniter and have multiple projects going on currently. I've made a redirect in my public_html to redirect to a directory called "home" right? And in this direct is the base for a codeigniter application where I have another .htaccess file removing the index.php from the url for controllers etc.
My question is are these two files conflicting one another? Here's my code:
public_html/.htaccess
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^myurl.co.uk$
RewriteRule (.*) http://www.myurl/home/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.myurl.co.uk$
RewriteRule (.*) http://www.myurl/home/$1 [R=301,L]
/home/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Also another question would be could I display my /home/index.php file on landing on myurl.co.uk without having home in the url?
In my public_html directory htaccess files I have
RewriteEngine on
RewriteCond %{HTTP_HOST} ^myurl.co.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.myurl.co.uk$
RewriteCond %{REQUEST_URI} !home/
RewriteRule (.*) /home/index.php/$1 [L]
And I changed my base url to http://www.myurl.co.uk/ whereas before I had http://www.myurl.co.uk/home. Ma bad!

htaccess remove folder from url only

im trying to remove only a specific folder from my url. i have see many answers here but i cant find any to work for me.
i have this url http://www.mydomain.com/projects/books/index.php
i want to just remove the projects folder so i can use
http://www.mydomain.com/books/index.php
i have this .htacces inside public_html folder :
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)projects
RewriteRule ^(.*)$ projects/$1 [L]
with this code i can access http://www.mydomain.com/books/index.php but any other address redirect me inside the projcets folder
for example http://www.mydomain.com shows me the index of/ projects
thank you
You can try this rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(projects/|index\.php) [NC]
RewriteRule ^(.+)$ /projects/$1 [L]

.htaccess rewrite subdomain to directory

Is it possible to use .htaccess to rewrite a sub domain to a directory?
Example:
http://sub.domain.example/
shows the content of
http://domain.example/subdomains/sub/
Try putting this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.example
RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]
For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.example
RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA]
I'm not a mod_rewrite expert and often struggle with it, but I have done this on one of my sites. It might need other flags, etc., depending on your circumstances. I'm using this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains/subdomain
RewriteRule ^(.*)$ /subdomains/subdomain/$1 [L]
Any other rewrite rules for the rest of the site must go afterwards to prevent them from interfering with your subdomain rewrites.
You can use the following rule in .htaccess to rewrite a subdomain to a subfolder:
RewriteEngine On
# If the host is "sub.domain.example"
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
# Then rewrite any request to /folder
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
Line-by-line explanation:
RewriteEngine on
The line above tells the server to turn on the engine for rewriting URLs.
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
This line is a condition for the RewriteRule where we match against the HTTP host using a regex pattern. The condition says that if the host is sub.domain.example then execute the rule.
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
The rule matches http://sub.domain.example/foo and internally redirects it to http://sub.domain.example/folder/foo.
Replace sub.domain.example with your subdomain and folder with name of the folder you want to point your subdomain to.
I had the same problem, and found a detailed explanation in http://www.webmasterworld.com/apache/3163397.htm
My solution (the subdomains contents should be in a folder called sd_subdomain:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} subdomain\.domain\.example
RewriteCond $1 !^sd_
RewriteRule (.*) /sd_subdomain/$1 [L]
This redirects to the same folder to a subdomain:
.httaccess
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.example$ [NC]
RewriteRule ^(.*)$ http://domain\.example/subdomains/%1
Try to putting this .htaccess file in the subdomain folder:
RewriteEngine On
RewriteRule ^(.*)?$ ./subdomains/sub/$1
It redirects to http://example.org/subdomains/sub/, when you only want it to show http://sub.example.org/.
Redirect subdomain directory:
RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
For any sub domain request, use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.band\.s\.example
RewriteCond %{HTTP_HOST} ^(.*)\.band\.s\.example
RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-z\-]+)
RewriteRule ^(.*)$ /%1/$1 [L]
Just make some folder same as sub domain name you need.
Folder must be exist like this: domain.example/sub for sub.domain.example.
Edit file .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]