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

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.

Related

Apache web server: URLs that contain a "." results in "Permission denied"

We are trying to migrate traffic from our old Netsuite store to a newer solution. The last step before launch is configuring the .htaccess file to redirect traffic from the old Netsuite URLs to the new SEF URLs.
Netsuite URL:
www.myoldstore.com/s.nl/sc.27/.f
New URL:
www.mynewstore.com/store-home-page
For launch, we will point the old domain "www.myoldstore.com" at our new server which is configured to accept traffic for this domain.
I configured the following .htaccess rules:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} ^www\.myoldstore\.com [NC]
RewriteRule ^ https://www.mynewstore.com/store-home-page [L,R=301]
However, I just discovered that any traffic on our server (Apache 2) that has a path like the old Netsuite URL (/s.nl/sc.27/.f) generates a "Permission denied" message.
Is there some way to configure either the server or .htaccess to allow/handle paths like that?
UPDATE: I found the problem. The new site is a Drupal site, and Drupal's default .htaccess file includees the following rule:
RewriteRule "(^|/)\." - [F]
That is intended to block access to hidden directories. I didn't want to remove it altogether, so I moved it lower in the .htaccess file so it only processes after the specific old URLs have been handled.
The new site is a Drupal site, and Drupal's default .htaccess file includes the following rule:
RewriteRule "(^|/)\." - [F]
That is intended to block access to hidden directories. I didn't want to remove it altogether, so I moved it lower in the .htaccess file so it only processes after the specific old URLs have been handled.

Apache URL rewrite ignored for a subdirectory

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.

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.

Apache Rewrite: domain specific gallery images in %{HTTP_HOST} directory

On shared web-hosting my software supports multiple domains (all domains point to the same public_html root directory). Each domain has it's own gallery (in the gallery directory). Since multiple domains can not have their own resources (e.g. images) in the same directory for all sites I have root directories that match each HTTP host name. Here is what the directory structure looks like...
/public_html/
/public_html/.htaccess
/public_html/gallery/
/public_html/www.example1.com/gallery/topic_x/image.png
/public_html/www.example2.com/gallery/topic_y/image.jpg
/public_html/www.example3.com/gallery/topic_z/image.gif
A request to...
http://www.example1.com/gallery/topic_x/image.png
...needs to be rewritten to...
/public_html/www.example1.com/gallery/topic_x/image.png
This needs to be done using the .htaccess file as is noted above, there are no .htaccess files in the HTTP Host matching directories (/public_html/www.example1.com/.htaccess does not exist).
I have been trying with numerous modifications of the following...
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(gallery)(/.+\.(gif|jpg|png))$ %{HTTP_HOST}$1$2 [L,NC]
...without success.
This one works for me:
RewriteRule ^(gallery/.+\.(gif|jpg|jpeg|png))$ /%{HTTP_HOST}/$1 [L,NC]
If you want -- you can add extra check to rewrite only if such final image is present (although I have not tested this):
RewriteCond {%DOCUMENT_ROOT}/%{HTTP_HOST}/$1 -f
RewriteRule ^(gallery/.+\.(gif|jpg|jpeg|png))$ /%{HTTP_HOST}/$1 [L,NC]
But even your pattern should work (at least it passes the test) -- maybe .htaccess is not enabled .. or you forgot to activate rewrite engine (RewriteEngine On)?
I would also recommend adding this line before (or after) activating engine:
Options +FollowSymLinks
or
Options +SymLinksIfOwnerMatch
Some shared hosting may only work with 2nd, for some 1st is enough.

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.