.htaccess map domain to a subfolder inside public_html - php

I have an apache server for the system https://theveganhut.co.uk ; I need to have different sites in the same server. So, I put the files for http://theveganhut.co.uk in a folder theveganhut.co.uk in the public_html folder.
I have mapped the domain theveganhut.co.uk to the folder theveganhut.co.uk via the .htaccess file in the root(public_html)
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?theveganhut.co.uk$
RewriteCond %{REQUEST_URI} !^/theveganhut.co.uk/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /theveganhut.co.uk/$1
# Change example.com to be your main domain again.
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?theveganhut.co.uk$
RewriteRule ^(/)?$ /theveganhut.co.uk/ [L]
But when I load the url http://theveganhut.co.uk/admin , the admin login page is seen, but the URL changes to http://theveganhut.co.uk/theveganhut.co.uk/admin/. I want the url to be http://theveganhut.co.uk/admin/
Is there something I am missing in the .htaccess file to address this issue? Please help.

Related

Remove Subfolder[Codeigniter] only for specified URLs using Htaccess

My Folder Structure
Root Domain
assets/ (contains css, images, js folders)
subfolder/ (contains Codeigniter Project inside and seperate .htaccess)
index.php
contact.php
.htaccess
Root Htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)subfolder
RewriteRule ^(.*)$ subfolder/$1 [L]
Subfolder(Codeigniter Project) Htaccess
DirectoryIndex index.php index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|assets|install|update)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For godady Shared Hosting Server uncomment the line below
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
List of Subfolder URLs
http://example.com/subfolder/products/
http://example.com/subfolder/product/product-slug-123
List of Root URLs
http://example.com/index.php
http://example.com/contact.php
In my Codeigniter Project I've set Products controller as default_controller.
So the problem is, when I try to access the index.php page in the Root folder it only displays the Codeigniter products list as Home page.
I need following URLs:
http://example.com/index.php
http://example.com/contact.php
http://example.com/products/
http://example.com/product/product-slug-123
Any solutions with htaccess or with code?
Finally I got this working.
RewriteCond %{REQUEST_URI} !((.*)subfolder|^/assets) // to Exclude this
RewriteCond %{REQUEST_URI} (^/products(.*)|^/product(.*)) // to Include this
RewriteRule ^(.*)$ subfolder/$1 [L]
Exclamation(!) symbol used to 'not' matching urls.
So I removed Exclamation symbol and used another RewriteCond (Condition) to redirect only if the url contains segment as products or product http://example.com/products/ or http://example.com/product/product-slug-123. It's working perfectly now. I thought, sharing this would be useful to someone. Happy coding.

.htaccess : Loads a directory by default

I have to upload a directory by default when someone comes to my static website
this is the structure
under my public_html I have domain folder 'domainname'. In domain folder I've webfolder/fr/index.html
I want to load files under webfolder/fr directly. following is my .htaccess under the domain name
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domainname$ [NC,OR]
RewriteCond %{HTTP_HOST} ^domainname$
RewriteCond %{REQUEST_URI} !webfolder/fr/
RewriteRule (.*) /en/$1 [L]
if I remove fr/index.html it loads files under webfolder. If I set
RewriteCond %{REQUEST_URI} !webfolder/fr/
it gives 500 internal error.
Please help me how can I fix this.
To Clarify:
When I open my domain www.domain.com I want it to open www.domain.com/webfolder/fr
Please add following two lines to allow a specific file to load just in case there is no default index file setup already
Options -Indexes
DirectoryIndex index.html index.php
Note : if you have to load first php index file then you have set like
DirectoryIndex index.php index.html
So you want to redirect your domain from root folder to some other subfolder
try this code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domainname$ [NC,OR]
RewriteCond %{HTTP_HOST} ^domainname$
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /webfolder/fr [L]
hope this code will work for you
optional:-you can also do this by changing your home directory in cpanel settings

.htacces redirect assets to assets folder

I have my PHP site setup with a folder for each site (e.g Login), which has an Index.php file and site-specfic assets. However, some of the assets, which are required by every single page, are stored in a 'Assets' folder located directly under the highest level (does that make sense?).
I have played around with .htaccess and got this code
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ http://janberktold.com/Assets/$1 [R=301,L]
However, my problem is: It redirects
localhost/Login/test.css
to
localhost/Assets/Login/test.css
instead of
localhost/Assets/test.css
How do I get my server to redirect to the correct path?
Try this
RewriteEngine on
RewriteBase /
# Rewrite if the file does not exists
RewriteCond %{REQUEST_FILENAME} !-f
# Rewrite only if the URI does not starts with Assets
RewriteCond %{REQUEST_URI} !^/Assets
# Rewrite any assets file
RewriteRule ([^/]*).(css|js|png|jpe?g)$ Assets/$1.$2 [L]
This should rewrite any assets files localhost/dir/file.css or localhost/dir/dir2/file.css to localhost/Assets/file.css
Replace your rules with:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?:[^/]*/)*([^/.]+\.(?:jpe?g|gif|bmp|png|tiff|css|js))$ /Assets/$1 [R=301,L,NC]

use subdomain to access codeigniter's modules

I have a sales script based on codeigniter and it's working great
I built an api for it, and am using it via: http://www.mysite/api/getdata
so I want now to access this api via a sub domain, like: http://www.api.mysite/getdata
as I think the solutions is: to redirect all *.mysite.com to mysite.com and then modify the .htaccess, but I don't know is it true?
----------------------------------------Update----------------------------------------
My current .htaccess File
<IfModule mod_rewrite.c>
# Check if mod_rewrite module loaded and we can work with SEO urls
SetEnv HTTP_MOD_REWRITE On
RewriteEngine on
# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# adapt and uncomment the following:
# RewriteCond %{HTTP_HOST} ^yoursite\.com$ [NC]
# RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
#RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^(.*)$ ./index.php [L]
</IfModule>
If you are using some kind of hosting service that lets you create a subdomain and point it to a document root, you can try creating a www.api.mysite domain and make the document root where mysite's /api directory is. That would get around the need to use an htaccess file. Alternatively, if you have access to your virtual host config, you can do the same thing, creating a vhost for www.api.mysite and point the document root to the /api directory.
If you don't have any control over that stuff, you'll need to setup the *.mysite.com to point to the same site as www.mysite and use an htaccess file. Something like this in the htaccess file in the document root should do the trick:
RewriteEngine On
RewriteCond %{HTTP_HOST} (www\.)?api.mysite [NC]
RewriteCond %{REQUEST_URI} !^/api/
RewriteRule ^(.*)$ /api/$1 [L]

How to load contents from different directory for www.website.com

How to display contents from www directory for http://www.website.com
i..e when a user visits http://www.website.com i want to display the contents from /www directory but keep the url same.
I have tried few methods, but for all of them the url changes as well....
Though i want to load the content from http://www.website.com/www/ i want to keep the url as http://www.website.com/
And same for other pages., i..e http://www.website.com/products.php : /www/products.php
You can use an htaccess file
RewriteEngine on
# Only apply to website.com URLs outside the www folder, but ignore real files and folders
RewriteCond %{HTTP_HOST} ^(www.)?website.com$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
# Finally redirect the topmost folder itself
RewriteCond %{HTTP_HOST} ^(www.)?website.com$
RewriteRule ^(/)?$ www/index.php [L]
If you are running apache, you can change the root folder in the httpd.conf file (UserDir)
http://httpd.apache.org/docs/2.0/mod/mod_userdir.html#userdir
So you would have to do something like:
UserDir /www/

Categories