.htaccess conflictions on different directories - apache

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!

Related

Deleting a directory from the path to the URL using .htaccess

There is a link like
domain.com/dir/page
It displays the contents of the page page.php, which is located in the dir directory of the site.
Current view .htaccess at the root of the site:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule .* http://%1/$0 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php/
RewriteCond %{HTTPS} =off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L]
ErrorDocument 404 /404.php
How do I replace the URL with domain.com/dir/page to domain.com/page (that is, remove dir from the path)?
P.S. It is understood that the solution should work for all URLs of the specified type.
Try using this in your .htaccess file, make sure it is above any other rules that you have:
RewriteEngine On
RewriteRule ^dir/(.*)$ /$1 [L,R=301]
Clear your cache before you test this.

.htaccess redirection to directories depends on URL

1) I have such directory structure:
.htaccess
album/
index.php
web/
.htaccess
index.php
...
...
In 'album' there is a gallery page and in 'web' there is a WordPress page.
How to fill .htaccess (I suppose in root dir) to redirect addresses depends on what is in URL:
www.domain.org -> goto /web and display wordpress page
www.domain.org/album -> goto /album and display gallery page
I've tried such thing in root .htaccess but without success (there is constant redirection to domain.org/web):
RewriteEngine On
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule ^(.*)$ /web
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org/album$ [NC]
ReWriteRule ^(.*)$ /album [L]
.htaccess in web directory is default of WordPress:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /web
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
How to solve this riddle?
Regards.
------------------
One of possible answer to thequestion (
RewriteEngine On
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule ^album(.*)$ album$1 [L]
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule web/$1 [L]
Weard is last line of this code. There are two spaces. I suppose first space means "empty string". This is strange (and also bad coding) however it works and I have enough of those redirections.
You can use these rules in root .htaccess:
RewriteEngine On
ReWriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
ReWriteRule ^((?!album).*)$ web/$1 [L,NC]

Redirect site in .htaccess

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]

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]

htaccess issue in codeigniter

I've 2 servers say x.com and x.net
x.com has CodeIgniter 1.7 and x.net has CodeIgniter 2.1
I'm using a htaccess to handle subdomain for the two servers
when I enter y.x.com it will go to x.com/y and y.x.net to x.net/y
But htaccess on the x.net server isn't working
the htaccess for x.net::
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^index\.php
RewriteCond %{HTTP_HOST} ^(.*)\.x\.net
RewriteCond %{HTTP_HOST} !^www\.x\.net
RewriteCond %{HTTP_HOST} !^x\.net
RewriteRule ^(.*)$ %1/%{REQUEST_URI}
the htaccess for x.com is the same just the site name is different
any idea, why is this happening?
I know the CI application folder was brought out of the system folder comparing v2.0 vs. v1.7
your .htaccess is how mine looked when I was using v1.7, that is:
RewriteRule ^(.*)$ /index.php?$1 [L]
For v2.0+, my .htaccess has a couple additional directives.
Therefore, can you try adding these to your .htaccess for x.net ONLY?
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]