Redirect site in .htaccess - apache

We have our LMS hosted on shared hosting server.
Our main domain is exampletraining.com
Our public_html folder has following folders inside it:
cgi-bin
lms
moodle
.htaccess
We added following lines to .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^exampletraining.com/$ [NC,OR]
RewriteCond %{HTTP_HOST} ^exampletraining.com$
RewriteCond %{REQUEST_URI} !lms/
RewriteRule (.*) /lms/$1 [L]
so that exampletraining.com should always show the content of exampletraining.com/lms
But our problem is that now we are unable to access exampletraining.com/moodle
What should we change in our .htaccess file so that we can also access exampletraining.com/moodle?
Thanks.

Just add an exception for moodle:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^exampletraining\.com$ [NC]
RewriteCond %{REQUEST_URI} !/(lms|moodle)/ [NC]
RewriteRule (.*) lms/$1 [L]

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^(/)?$ subfolder/index.php [L]
---
Within an htaccess file, the scope of the <Files> directive only applies to that directory (I guess to avoid confusion when rules/directives in the htaccess of subdirectories get applied superceding ones from the parent).
So you can have:
<Files "log.txt">
Order Allow,Deny
Deny from all
</Files>
In an htaccess file in your inscription directory. Or you can use mod_rewrite to sort of handle both cases deny access to htaccess file as well as log.txt:
RewriteRule /?.htaccess$ - [F,L]
RewriteRule ^/?inscription/log.txt$ - [F,L]

Related

Apache configured to ridirect urls to Joomla but then How can I still access Moodle?

I configured the .htaccess file like this:
Mod_rewrite in use.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/pathtopublichtml/joomla/
RewriteRule ^(.*)$ /pathtopublichtml/joomla/$1 [L]
But then when I try to get to /domain.com/moodle Apache takes me to Joomla. How Can I access both ?
You can add another RewriteCond to exclude any REQUEST_URI that starts with 'moodle'
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/pathtopublichtml/joomla/
# Make sure the redirect is not done for moodle
RewriteCond %{REQUEST_URI} !^moodle
RewriteRule ^(.*)$ /pathtopublichtml/joomla/$1 [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 - canonical URL when redirecting to subdirectory

I've been playing around with this problem for quite a while but can't find the way how to achieve what I want. I want user coming to my website test.com to reach index.php located in subdirectory www immediatelly, which I am able to do. But when the URL is test.com/www I want it to be just test.com.
Snippets of code:
.htaccess in /
RewriteRule ^(.*)$ /www/$1 [L]
.htaccess in /www
RewriteCond %{REQUEST_URI} ^/www.*$
RewriteRule ^/www/(.*)$ /$1 [L]
gives me 500 Internal Error.
UPDATE:
I came to a solution thanks to #Justin Iurman's answer:
.htaccess in /www
RewriteCond %{THE_REQUEST} \ /www/?(.*)\ HTTP/ [NC]
RewriteRule ^(.*)$ /%1 [R=301,L]
Have another problem tho. On the server machine I have multiple websites and I want to serve files according to HTTP_HOST variable in root .htaccess file. I redirect user accessing test.com to proper directory (say test), but I want URL test.com/test to redirect just to test.com. Directory test contains directory www where user is redirected and it does not stick to URL thanks to solution above. I would like to edit just .htaccess file in webserver root so I don't have any dependancy on websites' domains in projects.
SOLVED
.htaccess in webserver root:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect test.com/test (and everything into test folder) to test.com root
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteCond %{THE_REQUEST} \ /test/? [NC]
RewriteRule ^.*$ / [R=301,L]
# Forward (internally rewrite) test.com/one/example/page to test.com/test/www/one/example/page
RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
RewriteRule ^(.*)$ /test/www/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
.htaccess in the "test" directory:
<IfModule mod_rewrite.c>
RewriteEngine On
# allow /test in URL only for certain filetypes
RewriteRule ^(.*\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar\.gz))$ /test/www/$1 [L]
# allow /test on localhost
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteRule ^(.*)$ /test/www/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
Thanks a lot Justin!
You have to avoid infinite loop when doing what you want.
Put this code in your htaccess
RewriteEngine on
RewriteCond %{THE_REQUEST} \ /www/?(.*)\ HTTP/ [NC]
RewriteRule . /%1 [R=301,L]
RewriteRule ^(.*)$ /www/$1 [L]
EDIT: taking your update into consideration, here's the new code
RewriteEngine on
# Redirect test.com/test (and everything into test folder) to test.com root
RewriteCond %{HTTP_HOST} ^test.com$ [NC]
RewriteCond %{THE_REQUEST} \ /test/? [NC]
RewriteRule . / [R=301,L]
# Forward (internally rewrite) test.com/one/example/page to test.com/test/www/one/example/page
RewriteCond %{HTTP_HOST} ^test.com$ [NC]
RewriteRule ^(.*)$ /test/www/$1 [L]

Redirect another domain to folder without url changing htaccess

I have a htaccess redirect (without url change) to a folder where my site is. Recently i got a new domain name, but i can't get the htaccess code working on that domain.
main domain: www.test.com
folder of site: /drupal
this is the htaccess code i use (so users go to www.test.com and see the /drupal conten)
Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.test\.com$ [NC]
RewriteRule .* http://www.test.com/ [L,R=301]
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]
RedirectMatch 301 ^/drupal/$ http://www.test.com/
Now, i have a new domain www.secondtest.com on that server, which content stands on /drupal2 but whenever i type www.secondtest.com into my browser he redirects me to www.test.com .
Is there a way to modify the htaccess like so?
www.test.com (go's invisible to folder) --> www.test.com/drupal
www.secondtest.com (go's invisible to folder) --> www.test.com/drupal2
You have to use an another RewriteCond to check the domain :
# Redirection if not www.test.com nor www.secondtest.com
RewriteCond %{HTTP_HOST} !^www\.test\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.secondtest\.com$ [NC]
RewriteRule .* http://www.test.com/ [L,R=301]
# redirection to drupal folder if test.com
RewriteCond %{HTTP_HOST} ^www\.test\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]
# redirection to drupal2 folder if secondtest.com
RewriteCond %{HTTP_HOST} ^www\.secondtest\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal2/index.php?q=$0 [QSA]

mod_rewrite trouble - RewriteEngine On gives 403 when trying to access subdirectory

I've got a webshop(xtCommerce) running on a shared hosting webspace.
The root directory (where shop 1 is installed, up and running) contains the .htaccess file provided by the shop software
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/media/
RewriteCond %{REQUEST_URI} !^/extAdmin/
RewriteCond %{REQUEST_URI} !^/skin/
RewriteCond %{REQUEST_URI} !^/js/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php
I now installed a second webshop (also xtcommerce), within the same webspace, at /shop2 which contains the same .htaccess file
www.shop1.net -> /
www.shop2.net -> /shop2
-Trying to access shop2 results in 403
-When I'm removing RewriteEngine on from the .htaccess in /shop2 the frontpage displays just fine, but the links seem to be broken.
I don't know much about mod_rewrite, I tried adding RewriteBase /shop2 to the 2nd .htaccess file, but it failed.
Thank you in advance!
EDIT: Even when I use RewriteRule .* http://google.de in the root directory, and remove the .htaccess in /shop2 I keep getting "Forbidden". Maybe the problem isn't the rewriting?
EDIT2: Current status:
.htaccess in /
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/media/
RewriteCond %{REQUEST_URI} !^/extAdmin/
RewriteCond %{REQUEST_URI} !^/skin/
RewriteCond %{REQUEST_URI} !^/js/
RewriteCond %{HTTP_HOST} !shop2\.net$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php
.htaccess in /shop2 (4 different examples)
RewriteRule .* index.php # frontpage shop2.net works, any URLs to be rewritten result in 404
RewriteRule .* shop2/index.php # frontpage shop2.net works, any URLs to be rewritten result in 404
RewriteRule .* http://google.de # frontpage shop2.net works, any URLs to be rewritten result in 404
#RewriteRule .* index.php # shop2.net => forbidden
Rather strange setup of virtual host you have there. The problem is the htaccess in the root is executed before the one in /shop2. The root htaccess rewrites the the index.php file in the root. But because the document_root for shop2.net is /shop2, it is the index.php file is outside of the document_root, so apache forbids this (as it should).
To fix this put this in the root htaccess.
RewriteEngine on
RewriteCond %{HTTP_HOST} !shop2.net$
RewriteCond %{REQUEST_URI} !^/media/
RewriteCond %{REQUEST_URI} !^/extAdmin/
RewriteCond %{REQUEST_URI} !^/skin/
RewriteCond %{REQUEST_URI} !^/js/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php
But better just create a separate folder for shop2, instead of a sub-folder of shop1