How can I accomplish this type of mod_rewrite? - apache

I would like to setup a staging and production environment on one shared server. So, optimally, I would like to be able to create a structure like the following on the server:
/stage/
/lib/
/web/
/js/
/css/
index.php
...
/production/
/lib/
/web/
/js/
/css/
index.php
...
However, although I am able to change the document root of an added subdomain or addon domain, I am unable to change the document root of the primary domain. So, I am stuck with the document root being /public_html/.
I would like the staging environment to be accessible through stage.domain.com (pointing to /stage/web/), and every other subdomain, *.domain.com or domain.com, route to the production environment (pointing to /production/web/).
With that in mind, I believe I need a robust mod_rewrite script to do the job (.htaccess level). Since, I am a novice at mod_rewrite, does someone know how to write a script that will transparently route the requests appropriately?
or
Is there a better way to handle these two environments on a shared server?

If you can't change the document root of main domain. Can you create a symlink in public_html name production folder to /web/production. Enable symlinks.
RewriteCond {REQUEST_FILENAME} !-f
RewrireRule ^(.*)$ production/$1 [QSA,L]

Related

Specify to load index.html file for every request in apache config file

I have a React application hosted on my server and I need to always load index.html file for every request users make.
Let's say that I have a website that has the address xyz.com, and the root directory contains the React build files, including this index.html file. There are many routes that users can specify to access to certain parts of the website, for example to register on the website they can access xyz.com/register. So, what I want to accomplish is instruct server to always serve this index.html every time users access my site, even though they are visiting different routes of the website.
So I'm assuming that this is something that I can set up in the .conf file for the website, and if it is, can you please let me know how I can achieve it?
You can use the below rewrite rule.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule .* /index.html [L,R=302]

Howto use path/directory outside the domain root via symlink without access to domain config?

I am working an a shared hosting plattform which does not allow to edit or access the Apache config file.
The goal is to access the same files from two different domains which point to different domain roots:
test.example.com ---> /test_root/web
public.example.com ---> /public_root/web
Now I would like to access the same files using test.example.com/some/files/... and public.example.com/some/files/...
Of course I could simply copy the files to /test_root/web/some/files/... and to /public_root/web/some/files/... but this obviously just an example. In reality the files are a helpdesk system which should be integreated both into the test- and public-site. Copying the files would include maintaining two different systems, etc.
The goal is, to place the the files somewhere outside the two domain roots and make them available from both domains:
/test_root/web/some/files ---> /path/to/some/files
/public_root/web/some/files ---> /path/to/some/files
I created symlink to achive this (ln -s ...) but this does not work out. When I access on of the domains (e.g. test.example.com/some/files) I only get a blank page without any information what whent wrong.
I assume that the Apache is not configured to follow the symlinks. Without access to the Apache config I can neither check nor fix it.
Adding Symlinks to .../web/.htacess does not make any difference:
// test_root/web/.htaccess
Options +FollowSymLinks
So the question is: How can I make files outside the domain root available within a domain? Is this even possible? Is this possible using symlinks?
Add this in you .htaccess file.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} test.example.com/path/to/files [NC]
RewriteRule ^(.*)$ http://public.example.com/path/to/files [R=301,NC]
Test out this code. /path/to/files must be under /public_root/web/.
Something like /public_root/web/path/to/files/

.htaccess rewrite root to folder, yet block direct urls to same folder

I've been researching and trying for a week to accomplish this, but I haven't been able to find my solution. I'm sure it's possible, but I'm just not an expert in the depths of voodoo magic.
The setup:
An installation of MantisBT located in
mysite.com/mantisbt/currentver/mantis-1.3.19
When I perform an upgrade, I want to archive all old versions and old
database dumps to /mantisbt/oldversions/ to keep things tidy.
I also have other utilities and pages in various subdirectories, for
instance "mysite.com/utils"
The goal:
I want users to type in mysite.com/ (root) and have the URL rewritten
(transparently) to /mantisbt/currentver/mantis-1.3.19/ so that they
don't see my directory structure and it looks like the mantisbt app is
located in the root directory.
I also want protection from anyone trying to directly access a
subdirectory beginning with "/mantis". For instance, if
someone directly types mysite.com/mantisbt/currentver/mantis-1.3.19/
into their browser, I want them redirected back to the root directory
so they access the site from root just like everyone else.
I also need to allow my other subdirectories like mysite.com/utils to
be accessible if I type in the full path to them.
The biggest problem I've encountered is that Apache loops through your .htaccess file again every time the URL changes. So I get stuck in these rewrite loops. I've tried looking at every possible tool that Apache offers, but I'm seriously outgunned here. I could provide examples of what I've tried, they're obviously not correct, but ask me and I can post them.
You'd be far better off altering the DocumentRoot in your httpd.conf, and mapping in the utils directory, via a Location directive. It will be far cleaner, and Apache won't have to do some trickery with every request.
Anyway something along the following lines should work.
# Stop direct access
RewriteCond %{HTTP_REFERER} !mysite.com [NC]
RewriteRule /?mantisbt/currentver/mantis-1.3.19 / [NC,L,R=301]
# Internally Rewrite everything apart from /utils to /mantisbt/mantis-1.3.19/
RewriteCond %{HTTP_REFERER} !mysite.com [NC]
RewriteCond %{REQUEST_URI} !/utils [NC]
RewriteRule .* /mantisbt/currentver/mantis-1.3.19%{REQUEST_URI} [L]

Remove /public from URL in a subfolder

I'm building a Laravel application, and it will be in a subfolder of a current project. I'm trying to get rid of the /public in the URL, so that the users can view the project at this URL :
www.domain.com/project
instead of the default ...
www.domain.com/project/public
I did some digging on StackOverflow and the Laracasts forum, and I tried this, in a .htaccess file at the root of my project :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
However, my routes doesn't work anymore when I do this. Even the default route for / stopped working.
A lot of answers mention that the best way is to change the root in the Apache config, so that it serves the files from /public instead of at the root of the directory.
The problem is that this in a subfolder of a main project, how can I make it so that only this project is affected? The website at www.domain.com works fine and does not have a /public in the URL.
There are a couple options. I don't think you want to try fixing this at the htaccess level with mod rewrites (or anything like that)
One option is to create another virtual host that listens on an abitrary port (eg, 8080). Then set the document root for this virtual host to .../public/
Another option, you can spoof the dns in your /etc/hosts or Windows hosts file, and set up a dev.domain.com virtual host.
•Go to mainproject/public>>
a. .htacess
b. favicon.ico
c. index.php
d. robots.txt
e. web.config
1.cut these 5 files from public folder,and then paste on the main project folder that’s means out side of public folder… mainproject/files
2.Next after paste ,open index.php ,modify
•require __DIR__.'/…/bootstrap/autoload.php'; to
•require __DIR__.'/bootstrap/autoload.php';
modify
- $app = require_once DIR.'/../bootstrap/app.php'; to
- $app = require_once DIR.'/bootstrap/app.php';

Generic .htaccess for multiple websites stored in subdirectories

My development environment is set up for using a single host (localhost). I am developing multiple websites on my machine, each stored under its own directory like this:
/var/www/site1
/var/www/site2
...
The document root is set to /var/www on my machine.
I am using URL rewriting for most of these websites and most of the .htaccess files will rewrite a sub-directory to GET parameters in different ways like this:
http://localhost/site1/home/red -> http://localhost/site1/index.php?page=home&p1=red
http://localhost/site2/index/param1/param2/param3 -> http://localhost/site2/index.php?page=index&p1=param1&p2=param2&p3=param3
I also tend to copy some of these websites under different directories and, when I do that, I have to make a lot of changes in the .htaccess files for the website that I'm copying.
I would like to know if there is a way to define a constant that contains the website's root directory (not the host's document root) and how can that be used with the rewrite rule so that I would need to change only one line of code (setting this constant to a different value) when copying a website.
Putting this in a different form, is there a way to perform rewrites that relate to a website root instead of a host / %{HTTP_HOST} (i.e. the "host" for the website being localhost/site1 instead of localhost) and how can this be done?
I have tried removing the host from each request at the beginning of the script and prepending it back at the end of the script, but this does not work with rewrite rules that use the [L] option.
Thank you!
Regards,
Lucian
You could make an htaccess file with rules like this:
RewriteEngine On
RewriteBase /site1/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) index.php?page=$1&p1=$2&p2=$3&p4=$4 [L,QSA]
And put this in the directory /var/www/site1, and if you want for it to apply to site2, change the RewriteBase and put the rules in /var/www/site2.