Rewrite subdirectory to appear as another directory - apache

I have a folder containing an app in https://www.example.com/some/subdirectory/folder/. I'd like for visitors to be able to access this at simply https://www.example.com/app/. Essentially running all of the files from the /some/subdirectory/folder/ as if they were in /app/ (a virtual folder).
I've tried several variations of mod_rewrite and just can't seem to get it right.
Any help would be appreciated.
Thanks

Let's assume you need to access the content of the folder
/subdir/subsubdir
under the virtual folder
/app
This can be done by the following .htaccess file placed in the webroot of your application:
RewriteEngine On
RewriteCond "%{ENV:REDIRECT_rewritten}" "!=1"
RewriteRule ^/?subdir/subsubdir/(.*)$ /app/$1 [R=301,QSA,L]
RewriteRule ^/?app/(.*)$ /subdir/subsubdir/$1 [QSA,L,E=rewritten:1]
Here you can find more information on url-/folder-mapping and also a quick overview concerning mod_rewrite:
https://mod-rewrite-cheatsheet.com/#url-mapping

Related

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.

Drupal: how rewrite all urls so that the instal directory isn't included in the link

I installed drupal on my site at say, example.co.uk/portfolio.
All the links are showing as example.co.uk/portfolio/project-1.
I want it to be example.co.uk/project-1
How can I use rewrite rules or whatever so that wherever there is a link that has the /portfolio directory showing, that it will not show it in the href at all? I want to be able to hide that the site is installed in a directory and want it to show everything from the root url www.example.co.uk/
So far,
I've messed around with RewriteBase / and also mapping the portfolio to the main root via:
RewriteRule ^/?$ /portfolio
Try this mod_rewrite rule in the .htaccess file in your root directory:
RewriteEngine on
RewriteRule !^portfolio/ portfolio%{REQUEST_URI}