Redirect entire directory as my home directory with mod_rewrite - apache

I've been looking for an answer to this for a while and I've tried many tricks myself, but to no avail, so I bring my questions to all you wise people.
I have a website all ready to go. The entire contents of this website is in the main "www" directory of the site. So now when I go to www.mywebsite.com/main, it goes to the www/main.php file. However, I want to move the entire contents of my website into a new folder inside the www directory, which I will call "ts". So now, my main.php file will be in www/ts/main.php
However, the problem I am having is I want all URIs to remain the same, so I want to tell the server that www.mywebsite.com/main is now found at www/ts/main.php instead of where it was before (www/), and that all subsequent content is also now found in www/ts instead of www/.
I imagine that I will need a .htaccess file in my www/ directory which tells the server that the absolute path of www.mywebsite.com is found at www/ts/, but I am not sure how to do that. I also have an .htaccess file inside my new www/ts/ directory already redirecting many URIs, but I imagine that I need to tell the server that all the files within the directory need to ignore the /ts/ URI, again, I am not sure how to do that.
If anyone is able to point me in the right direction, I would be much obliged. Thanks.
EDIT: I am on a shared server so I do not have access to any of the conf files, therefore I need a solution involving mod_rewrite instead. Thanks.

You just need to specify where is the DocumentRoot of your VirtualHost.
Check Apache configuration for your Virtualhost (it's maybe the default one, but you could create one for your named site). And check the DocumentRoot tag.
If it was something like:
DocumentRoot /foo/bar/www
Alter it to :
DocumentRoot /foo/bar/www/ts
And that's all.
Edit:
With the restriction of only accessing a .htaccess and if we assume you will limit this new folder redirection for only one DNS name (as if you make a subdir it's maybe to make some other subdirectories later) this should work:
RewriteEngine On
# limit this rule on the www.mywebsite.com DNS name requested
RewriteCond %{SERVER_NAME} ^www\.mywebsite\.com$
# limit infinite recursion, when filename is ok...
# no, in fact not # RewriteCond %{REQUEST_FILENAME} !-f
# well this one is better, no loop for mod_rewrite, even on 404
# the rule is applied only once
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# so here's the rule, map the requested file on the filesystem in ts subdir
RewriteRule ^(.*)$ /var/www/ts/$1 [L]

Related

.htaccess RewriteRule behavior with existing subdirectories

I've been through many of similar questions but I couldn't find this particular case:
Having this structure:
public_html/
q/
.htaccess
index.php
/dirnofixedname1
/dirnofixedname2
/dirnofixedname3
dirnofixednameN are folders that have files to be used by index.php and not to be directly accessible (called like that as I may not enumerate all in the .htaccess file or it would be impractical)
index.php should process incoming requests
The intention is to process requests like:
http://domain/q/dirnofixedname2 with http://domain/q/index.php?q=dirnofixedname2 while still showing http://domain/q/dirnofixedname2. A popular and already solved case indeed.
So that .htaccess file is:
RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?q=$1 [L]
The problem happens that when the request matches those existing directories (thing I want), it works as intended (index.php executes and gets q) but making a redirect to:
http://domain/q/dirnofixedname2/?q=dirnofixedname2
(and showing that in the URL bar), instead of the intended:
http://domain/q/dirnofixedname2
Particularly, if the directory happens to not exist,
http://domain/q/dirthatdoesnotexist
Gets processed correctly by index.php with q as dirthatdoesnotexist (the script obviously dismisses that and returns nothing).
Do you have any ideas about how to avoid that redirect in cases where the subdir exists? (It's practical to have the same dir name as the parameter)
This is happening due to DirectorySlash directive which is by default in On state since you are requesting an actual directory in your URI.
You can turn it off by using:
DirectorySlash Off
Also to mask directory listing use:
Options -Indexes
at top of your .htaccess

htaccess for laravel without affecting subdomains

I'm trying to get htaccess working for laravel 5.4, but without it affecting the sub-domains that are created.
My current htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
What I've tried is setting an htaccess in the sub-domain folders with the follwing:
RewriteEngine On
As I read elsewhere this should stop the top domain htaccess, yet when for example I have a sub-domain like dev.example.com, it will redirect to dev.example.com/dev
Anyway of getting rid of the /dev at the end?
Folder structure:
app
bootstrap
config
database
public
storage
resources
routes
dev --> subdomain
Upfront, I don't see how this /dev is related to the directives, you have shown. None of them do anything to add a /dev anywhere. Maybe it's just the subdirectory applied somehow.
The claim "this should stop the top domain htaccess" is not entirely true. From Apache - How directives are applied
The configuration directives found in a .htaccess file are applied to the directory in which the .htaccess file is found, and to all subdirectories thereof.
However, it is important to also remember that there may have been .htaccess files in directories higher up. Directives are applied in the order that they are found.
Therefore, a .htaccess file in a particular directory may override directives found in .htaccess files found higher up in the directory tree. And those, in turn, may have overridden directives found yet higher up, or in the main server configuration file itself.
So an .htaccess doesn't stop another .htaccess, but a directive overrides a directive. This means, you may have some directive from one .htaccess and another unrelated directive from the top level .htaccess.
In your case, RewriteEngine on just overrides RewriteEngine on from the main .htaccess file.
If you want to prevent any RewriteRule from the top .htaccess, I would rather try
RewriteEngine off

Two sites/Two domains in one server

I'm starting to manage an Apache web server and I have very little experience. I have two websites with two different domains (a.com, b.com).
A.com files is on the server root folder (/htdocs) and I have a .htaccess file configured for that domain already.
B.com is on a subfolder inside the server root (/htdocs/b/).
Therefore, depending on the "incoming" request domain, I want to somehow change the folder on the server (using .htaccess maybe?).
I have been reading some material on this, such as this tutorial, Apache guide, this blog post, and even this StackOverflow question, but nothing seems to have worked so far.
Changing the .htaccess is the correct thing to do? Can I do a mod_rewrite without the user noticing that the folder has been changed (keep it as "b.com" and not "b.com/b")?
Thanks in advance!
You can this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# if host is b.com
RewriteCond %{HTTP_HOST} ^(www\.)?b\.com$ [NC]
# silently forward to folder /b/ if not already /b/
RewriteRule !^b(/|$) /b%{REQUEST_URI} [L,NC]
PS: Though it will be better to change your VistualHost config and set /htdocs/b/ as DocumentRoot for b.com site.

Apache .htaccess to redirect index.html to root, why FollowSymlinks and RewriteBase?

In order to redirect all somefolder/index.html (and also somefolder/index.htm) to somefolder/ I use this simple rewrite rule in Apache .htaccess file:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^.*\/index\.html?\ HTTP/
RewriteRule ^(.*)index\.html?$ "/$1" [R=301,L]
This works well!
But at Google groups they suggest to add also:
Options +FollowSymlinks
RewriteBase /
Could anyone be so kind to explain me why would i have to add these last lines, and explain me a bit what they mean and what they do?
Is there a potential secuirty risk in not adding these lines?
Many thanks,
Why they're suggested:
It's suggested that you add Options +FollowSymlinks because it's necessary that symlink following is enabled for mod_rewrite to work, and there's a chance that, while you may be allowed to turn it on, it's not enabled by the main server configuration. I suspect the reason that symlink following is necessary is beause the module makes a number of calls to apr_stat(), which looks like it needs to follow symlinks in order to get file information in all cases.
As for RewriteBase, it's typically not necessary. The documentation goes on about it, but as most people's files do live under the DocumentRoot somewhere, it usually only serves a purpose if you're redirecting externally and you use directory-relative URLs. To illustrate what I mean, consider the following:
RewriteEngine On
RewriteRule ^redirect index.html [R,L]
A request for example.com/redirect will result in an external redirect to example.com/full/path/to/web/root/index.html. The reason for this is that before it handles the redirection, mod_rewrite re-appends the current directory path (which is the default value of RewriteBase). If you modified RewriteBase to be /, then the path information would be replaced with that string, so a request for index.html would now be a request for /index.html.
Note that you could just have done this explicitly on the replace too, regardless of the value of RewriteBase:
RewriteEngine On
RewriteRule ^redirect /index.html [R,L]
...works as intended, for example. However, if you had many rules that needed a common base and were being shifted around between directories, or your content wasn't under the root, it would be useful to appropriately set RewriteBase in that case.
The risk of not using them:
There's absolutely no security risk in not specifying Options +FollowSymlinks, because if you don't and it's not set by the main server configuration, mod_rewrite will always return 403 Forbidden. That's kind of problematic for people trying to view your content, but it definitely doesn't give them any extended opportunity to exploit your code.
Not setting RewriteBase could expose the path to your web content if you had an improperly configured rule set in one of your .htaccess files, but I'm not sure that there's any reason to consider that a security risk.

Problems redirecting old domain to new with Apache and htaccess

My homepage is located at www.nazgulled.net and I bought a new domain which is www.ricardoamaral.net. Both these domains point to the same exact server.
I'm having two problems with the redirection, my current code is this:
RewriteCond %{HTTP_HOST} ^(www\.)?nazgulled\.net [NC]
RewriteRule ^(.*)$ http://www.ricardoamaral.net/$1 [L]
For now I'm just testing but the idea is to replace [L] with [L,R=301] when I'm ready to move the whole thing, just so you know.
Anyway...
1) This is not working, when I try it, I can't access "nazgulled.net", it gives me a "server internal error" and I don't understand why... I don't understand why because if replace "ricardoamaral.net" by "google.com", the redirect works just fine :/
2) I have a few subdomains and I would like to redirect everything in those too. My first choice is to add different rewrite conditions/rules for each of the subdomains but that takes a lot of manual code and if the user types some subdomain that doesn't exist, they don't be redirect it and I also want that.
I think for your situation creating separate vhosts would be the ideal fit. What I do often is place domains that are being redirected on the same server but in a different folder to keep the www folder clean, for instance. My main site would be here:
/var/www/example.com/public
For all my redirects I would place them like so:
/var/www/redirects/example-2.com/public
Within each public folder of the redirected sites you would add this line to your .htaccess file
RedirectMatch permanent /.* http://example.com/
For 1, you can use something like this instead:
<VirtualHost *:80>
ServerAdmin webmaster#nazgulled.net
ServerName nazgulled.net
ServerAlias www.nazgulled.net
Redirect permanent / http://www.ricardoamaral.net/
</VirtualHost>
I use a similar configuration and it works perfectly, keeping query parameters, paths, etc.
Regarding your own rewrite rule, I want to note that you may lose your query params if you don't include QSA.
To diagnose internal server errors, just have a look at the log file provided by your ErrorLog directive within your vhost, or a global one, depending on your configuration. It should give you the exact reason for the error.
This is best solutions. Create a notepad file and save it as .htaccess if you do not already have an existing one.
Update the .htaccess file with the following code and save
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://new-domain.com/$1 [R=301,L]