Apache URL rewrite ignored for a subdirectory - apache

I'd like to publish a maintenance message for one of the subdirectories on my domain. This code is placed in the /.htaccess of the domain:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/webmail/
RewriteCond %{DOCUMENT_ROOT}/webmail-maintenance.html -f
RewriteRule $ /webmail-maintenance.html [L]
If webmail-maintenance.html exists in the root directory, it should be served for whatever was requested from the webmail directory. It has no effect though. If I replace the first line with another directory name, like test, and create a new directory named test with a single index.html in it, the above configuration has the desired effect. It just doesn't work for the webmail directory.
There is another .htaccess file in the webmail directory, but it doesn't contain contradictory rewrite rules. Here's its contents. Is rewriting not supported at all if the subdirectory contain a .htaccess file itself?
The web server is Apache 2.4 on Linux.

Related

.htaccess how to serve contents from different directory for mobile and desktop?

I have two version of my site. one is a mobile version and another one is desktop.
Path looks like this :
For Mobile : public_htm/mobile
For Desktop : public_html
I want to change the document root of my domain based on the device. I want to serve content from the mobile folder if the user's device is mobile & serve contents from public_html if user on desktop.
How can I do this with .htaccess?
I want to have my domain the same for both desktop and mobile, only document root changed by .htaccess based on the device.
You would need to add something like the following to the top of the root .htaccess file:
RewriteEngine On
# Prevent direct access to the "/mobile" subdirectory
# - redirect back to the root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^mobile/(.*) /$1 [R=301,L]
# Rewrite all requests from mobile devices to the "/mobile" site
RewriteCond %{HTTP_USER_AGENT} ^iphone|android|etc$ [NC]
RewriteRule ^(?!mobile/).* mobile/$0 [L]
The mobile detection regex (RewriteCond directive) is just an example, the specifics are left up to the reader. (For reference: mobile browser user-agent strings)
The negative lookahead in the RewriteRule pattern prevents requests that have already been rewritten to the /mobile subdirectory from being repeatedly rewritten. If you have another .htaccess in the /mobile subdirectory containing mod-rewrite directives then this may be unnecessary.
The first rule redirects any direct traffic to the /mobile subdirectory back to the document root. However, if you have another .htaccess file in the /mobile subdirectory containing mod-rewrite directives then this redirect would need to be moved to that .htaccess file (it won't work here).
Just a note on terminology... This doesn't strictly change the "document root", as mentioned in the question. (Although it might "look like" it does from a client perspective.) This internally rewrites requests to a subdirectory (URL-rewriting). Server-side applications running in the /mobile subdirectory still see the document root as being the parent directory. You cannot change the "document root" in .htaccess, which can only be done by setting the DocumentRoot (or VirtualDocumentRoot) directive(s) in the server config.

The folder "www" doesn't seem to work properly on hostgator

I've developed a website using Yii framework and now I need to move it to hostgator cheap hosting for a single site. It looks like it expects that the website must be placed into the root folder but my website has a www folder with index.php and resource files like js,css,images,etc. Also this folder contains a file htaccess with following content:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
But I suppose I have to add another htaccess into the root folder so all requests will redirect to the www folder. (I did it and tried different combinations of the rewrite rules but they didn't work properly). I'm guessing I should remove htaccess from the www folder and leave only one in the root folder.
Sorry guys, I always have problems with htaccess even after reading documentation.
Thanks in advance.
I think that you can configure your domain as an addon domain with the document root set in www as you prefer.
Of course I am presuming that you have cPanel on Hostgator.

Change document root folder in shared server with .htaccess

I'm the admin of an apache server (on a hosting package that allows me to host multiple domains), I've got one domain in public_html (let's call it www.ROOTwebsite.com) and 9 other domains hosted in a folder in the same directory level as the public_html, called DOMAINS.
So the structure is:
-DOMAINS/site1.com/
/site2.com/ ... etc
-public_html
I'm using '/' in the beginning of all relative paths in wamp for site1.com (for example /menu.php) and it works fine, but when I upload to DOMAINS/site1.com/ it messes up the site because it obviously is looking at the public_html directory as the ROOT.
I've used a number of combinations on the following in the .htaccess file, but I can't figure out which is the right syntax to change the ROOT to a directory sitting NEXT to public_html, not under it as usual:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.ROOTwebsite.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^ROOTwebsite.gr$
RewriteCond %{REQUEST_URI} !DOMAINS/site1.com/
RewriteRule (.*) /DOMAINS/site1.com/$1 [L]
I wish to change the root directory for site1.com so that it also works with the '/', without affecting the public_html website.
Can anyone see the problem with the code above?
You cannot redefine the document root via .htaccess. You have to create different vhosts for the domains that then can have separate document roots.
The closest I have come to redefine a document root via .htaccess is the following (not exactly what you are asking, because in my example, site1.com is a subdir of the main document root, but this would achieve what you want, with the caveat below).
RewriteCond %{HTTP_HOST} ^site1.com$ [NC]
RewriteRule !^site1.com/ /site1.com%{REQUEST_URI} [L,NC]
this will effectively redirect all site1.com to the site1.com subdirectory.
[caveat] The only problem I could not solve is how to redirect an url that goes directly to the subdirectory such as http://site1.com/site1.com/index.html to http://site1.com/index.html

RewriteRule to point to subdirectory

im trying to install phprojekt 6.0.4.
I see the directory structure doesnt allow to put the whole content into my public html folder and directly access it, as it has a htdocs folder in which normally my vhost docroot conf should point to.
So i want to change the .htaccess file in the root directory so every access to the root directory internally redirects to htdocs/ folder.
Im not able to get this running without 500 INTERNAL Errors.
I use these two lines to redirect.
RewriteEngine On
RewriteRule / htdocs/
So i whenever someone access the root folder of phprojekt, apache should point to htdocs/, it like trying to change the docroot from the root to the subfolder htdocs/.
How do i get this running?
Assuming you're not able to just change the DocumentRoot, try something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !htdocs
RewriteRule (.*) htdocs/$1

Setting up Drupal and Wordpress under a single document root

I have a hosting account which provides me a folder to publish my files for my domain (say www.example.com). I have set up Drupal for www.example.com with .htaccess at the top folder to enable clean-urls for the Drupal installation. Now I want to have a Wordpress installation under www.example.com/blog/ and have clean URLs for that blog. But while using .htaccess it is not working ok as the .htaccess at the top folder will override the sub-folder one. How to achieve what I intend to?
This really depends on the exact content of your respective .htaccess files.
One workaround is to add a RewriteCond to the head of the main .htaccess file that, if the request URI matches the sub-directory, stops parsing:
RewriteCond %{REQUEST_URI} ^/blog
RewriteRule .* - [L]
this should lead to the blog URLs being parsed properly, based on the rules specified there.