RewriteRule to point to subdirectory - apache

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

Related

How do I host my website with the index file being in a subfolder?

I would like to host my website from /src/index.html
So if you visit my website at https://example.com, you will see the contents of /src/index.html (without adding /src/index.html to the url)
This is currently my file structure
I tried adding the following in my .htaccess file:
RewriteRule ^ src/index.html
That redirects correctly but also redirects all paths linking to any other files (like style.css) to src/index.html making them unusable.
Something like the following would work but it would change the url to example.com/src/index.html while I would like it to stay at example.com:
Redirect 301 /index.html /src/index.html
RewriteRule ^ src/index.html
This naturally rewrites everything. To rewrite requests for the root only then you need to match against ^$ instead.
For example:
RewriteRule ^$ src/index.html [L]
The easiest way is to put your project elsewhere, outside of DocumentRoot, and make a softlink (ln -s) to your src folder in DocumentRoot.
If you want your project to be the top level (as in your example, http://example.com/), then you can directly set DocumentRoot to your src folder, or replace the DocumentRoot folder with a softlink as described above.

Rewrite subdirectory to appear as another directory

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

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.

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

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}