Using mod_rewrite to redirect .dev domains - .htaccess

In the Apache 2.4 docs on dynamic virtual hosts, it says:
Mass virtual hosts with mod_rewrite
Mass virtual hosting may also be accomplished using mod_rewrite, either using simple RewriteRule directives, or using more complicated techniques such as storing the vhost definitions externally and accessing them via RewriteMap. These techniques are discussed in the rewrite documentation.
I'm attempting to use mod_rewrite instead of mod_vhost_alias because I want it both ways: localhost/project and project.dev should point to the same folder, but either URL should work.
Here's my latest attempt (currently in an .htaccess), which gets me a lovely 500 error.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.dev$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
If I do
...
RewriteRule ^(.*)$ http://localhost/%1/$1 [L,QSA]
I can access the files, but the URL changes (not what I want). I've tried a variety of permutations with and without slashes, RewriteBase, etc.
To be clear, I want project.dev/index.php and localhost/project/index.php to both be valid non-redirected references to /var/www/html/project/index.php. And I'd like to do this in a dynamic way, so I don't need to enter a new set of rules for every folder.
I'm not fixated on doing this with .htaccess - virtualhosts are ok too as long as they're dynamic and I can still access my sites using the localhost/ scheme and the other machines on the network can connect to the sample sites in the usual way (192.168.1.22/project/index.php).

Try this rule:
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(.+)\.dev$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L]

Related

Drupal 9 multisite split .htaccess

I am working on Drupal 9 multisite and I want split the .htaccess file, one per site to create different redirect rules.
I know that it's possible to add all rules on drupal_root/.htaccess but I prefer to have one .htaccess per site.
The structure is:
drupal_root/sites/site-1/
drupal_root/sites/site-2/
drupal_root/sites/site-3/
drupal_root/sites/site-4/
and i want add .htaccess, for example in drupal_root/sites/site-1/ and another different in drupal_root/sites/site-2/
I tried adding .htaccess directly on drupal_root/sites/site-1/ but it doesn't work.
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.dev.{site-1}.com/test/?$ [OR]
RewriteCond %{HTTP_HOST} ^dev.{site-1}.com/test/?$ [NC]
RewriteRule ^(.*)$ https://www.dev.{site-1}.com/testredirect [R,L]
</IfModule>
Presumably the URLs used to access "site-1" don't actually contain /sites/site-1/? In which case the URL would need to be internally rewritten (by Apache) to this directory (or a subdirectory of) for the /sites/site-1/.htaccess file to be processed (by Apache) - but I don't believe that happens (since everything is handled by the core Drupal installation in the root - the purpose of having a "multisite" installation).
So, I don't think what you are suggesting is realistically possible I'm afraid (without more work).
Ordinarily you would need to have multiple "separate" Drupal installations to be able to have separate .htaccess files.
This would seem to be backed up by the following question on the Drupal SE site, which suggests to use the "Redirect module" to manage per site redirections.
Drupal Stack Exchange - Per site htaccess with multisite
Aside:
RewriteCond %{HTTP_HOST} ^www.dev.{site-1}.com/test/?$ [OR]
RewriteCond %{HTTP_HOST} ^dev.{site-1}.com/test/?$ [NC]
RewriteRule ^(.*)$ https://www.dev.{site-1}.com/testredirect [R,L]
This rule wouldn't do anything anyway, since the HTTP_HOST server variable contains the hostname from the request only, not the URL-path. But would you need to check the hostname if this .htaccess file is intended only to be processed when the user makes requests to "site-1"?
For example, it should be written:
RewriteCond %{HTTP_HOST} ^(www\.)?dev.{site-1}\.com.?$ [NC]
RewriteRule ^test/?$ https://www.dev.{site-1}.com/testredirect [R,L]

How to redirect root domain to subfolder (with https) and rest of addon domains to subfolders (without https)

How to redirect root domain to subfolder (with HTTPS) and rest of other addon domains to subfolders (without HTTPS).
Currently I have this .htaccess in root which redirects with HTTPS to the-main-subfolder ok. But my other addon domain, say domain2 also gets redirected to the-main-subfolder.
I would like to redirect domain2 to the-domain2-subfolder without HTTPS.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RedirectMatch ^/$ /the-main-subfolder/
I am not sure if this code is correct as it might me using a wildcard. I got this code from searching on net but there are so many suggestions that I am confused now!
In summary: My main hosting account in root should go to https://www.domain1.co.uk/the-main-subfolder when user types in domain1.co.uk in browser and my addon domain http://domain2.co.uk should go to http://www.domain2.co.uk/the-domain2-subfolder.
You can use additional RewriteConds to define specific redirections:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^example.org$
RewriteRule ^ https://%{HTTP_HOST}/the-main-subfolder%{REQUEST_URI} [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^example1\.org$
RedirectRule ^(.*)$ /example1\.org-subfolder/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^example2\.org$
RedirectRule ^(.*)$ /example2\.org-subfolder/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^host1\.example\.org$
RedirectRule ^(.*)$ /host1\.example\.org-subfolder/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^host2\.example\.org$
RedirectRule ^(.*)$ /host2\.example\.org-subfolder/$1 [L,QSA]
I added a few examples to demonstrate the redability of explicit implementation and that you can do that for both, separate domains and hostnames (sometimes incorrectly called "subdomains"). I would always prefer such explicit notation over generic approaches since you can individually modify things, for example for testing or debugging purposes. Except if you are in a mass hosting situation obviously, then a database based approach makes sense.
Note that the redirection for what you call the "root domain" (example.org here) has a second RewriteCond now. Both conditions are AND-combined per default.
For safety you probably also want to add some more rules to redirect requests to something like https://example.org/host1.example.org-subfolder to the specific domain name, since according to your description you are limited to a single file tree in your hosting account. Same for request to http://test1.example.org/test1.example.org-subfolder/... to eliminate the literal folder name.
Oh, and a warning: the above syntax works for .htaccess style files only. If you have access to the real host configuration then you should always prefer to place such rules in there. However you need a slightly changed syntax then. .htaccess style rules are notoriously error prone, hard to debug and they really slow down the http server. They are only offered as a last option for those without access to the host configuration.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain1.co.uk$
RewriteRule ^ https://%{HTTP_HOST}/the-main-subfolder%{REQUEST_URI} [L,R=301,QSA]
Thanks #arkascha - Everything now works as expected with the above code. I suppose we do not need to mention so called add-on domains here at all because cPanel handles the sub-directories for them internally when we add subsequent domains on the hosting package (i.e. addon domains)!
Just to update that my previous solution partially works as it has few niggles/bugs. So went back to the drawing board and suddenly realised I was unnecessarily trying too hard!!
Deleted the old htaccess file first and followed instruction below..
The solution is already provided by cPanel in something called "Redirects" in Panel Icons.
I just had to enter everything in user interface text boxes like choose domainname = "domain1", old folder = "\", new folder = "https://www.domain1.co.uk/the-main-subfolder" - And just click create the redirect. In doing so it creates a .htaccess file itself automatically. I am sharing this below:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.co\.uk$
RewriteRule ^/?$ "https\:\/\/www\.domain1\.co\.uk\/the-main-subfolder\/" [R=301,L]

htaccess on one.com webspace

Recently I moved my websites to the hoster one.com. They have setup an automated mechanism (I dunno what they use to achieve that) to rewrite any first-level folder on the webspace to a subdomain.
I.e. the folder http://example.com/folder1/ will be also available as http://folder1.example.com/
Now, I have a site, that is using quite a lot javascript to include pages from a hardcoded, static source. Due to the SOP the scripts are working depending on which hardcoded reference they use.
So, to make sure that everybody gets a working version of the website, i wanted to redirect the direct folder access to the subdomain as well.
My htaccess for this - which is working localy and on various htaccess-testers out there - seems to be not working with one.com:
RewriteEngine On
#Rewrite Access to folder1-folder to subdomain.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder1.*?$ [NC]
RewriteRule .* http://folder1.example.com/ [R=301,L]
Since I don't know the exact mechanism one.com is using to achieve the mentioned behaviour it might just be a conflict with my rules.
Support says, that all the used commands are fully supported, and therefore wasn't be able to tell what's going wrong...
Does anybody have encountered something similiar and has a hint for me?
just fiured out the solution:
RewriteEngine On #does not work
vs.
RewriteEngine on #does work
You need to check that the actual request was made for /folder/ and not the URI (which can internally be rewritten). Try:
#Rewrite Access to folder1-folder to subdomain.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /+folder1/ [NC]
RewriteRule ^folder1/(.*)$ http://folder1.example.com/$1 [R=301,L]

htaccess subdomain rewrite without a redirect

Using htaccess Rewrite, I want my url http://*.phoneataxi.com/ (where * is a wildcard, excluding 'www') to show in the address bar as is but get information from http://*.phoneataxi.com/test.php?c=*.
I have tried so many different things but nothing is doing exactly what I need. Most examples are redirecting the subdomain to the '/test.php' file in the address bar which I don't want to do.
I'm trying not to have to create individial subdomains and subdomain folders within my webroot.
Ideas?
I use this htaccess file to make Apache act as a proxy for another host:
IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ghost\.pileborg\.se$
RewriteRule (.*) http://vps.pileborg.se/ghost/$1 [P]
</IfModule>
It causes all access to http://ghost.pileborg.se/ to be "redirected" to http://vps.pileborg.se/ghost/.
UPDATE (2020)
Some of the answers regarding this topic is very old and no longer work as expected.
After searching for hours on something that actually works, this is what I came up with; edit as you see fit:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ([a-z0-9]+)\.
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.php -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.html -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.html [L,NC,QSA]
Breakdown
Make sure that the rewrite module is installed and enabled on your host
first we turn the rewrite engine on and set the path-base
then isolate the subdomain - any letters/numbers before the first dot
set a variable in this runtime environment that contains the subdomain
check if the subdomain folder and index-file exists
if it does exist -then use that file as the request-handler (no redirect)
if it does not exist then the request carries on normally
Flags
The flags used here are explained here, but the ones used above are quite simple:
[L] Last rule, ignore the rest
[NC] No Case, no uppercase/lowercase restrictions
[QSA] I remember this as "Query String Attach" :D

Subdomain alias with htaccess

I have this code for managing a subdomain alias: browsing sub.domain.com it shows domain.com/fp/sub.
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$
RewriteRule (.*) /fp/sub/$1
I have a lot of these subdomain to setup. Is there a way to automate this process placing some variables instead of the correct subdomain/folder name?
I tried this code with no success
RewriteCond %{HTTP_HOST} ^(sub1|sub2)\.domain\.com$
RewriteRule (.*) /fp/$1/$2
Francesco, first correcting your example, the rule should be:
RewriteRule ^(.*) fp/%1/$1 [L]
The % variables are set in the last successful cond regexp match.
Second there are lots of options if you have access to the system or vhost config, such as using Rewrite Maps or mass virtual hosts. If you don't have such access are limited to .htaccess, then you need to use one or more rules much as you are already doing. One variant is to use an existence check of the target directory, such as:
RewriteCond %{HTTP_HOST} ^(\w+)\.domain\.com$
RewriteCond %{DOCUMENT_ROOT}/fp/%1 -d
RewriteRule (.*) fp/%1/$1 [L]
Beware that if you are using a shared hosting service, that DOCUMENT_ROOT may not be properly set at rewrite execution, in which case your SHS provider will set up an environment variable to do the same, e.g. %{ENV:DOCUMENT_ROOT_REAL}. Do a phpinfo to find out. See Tips for debugging .htaccess rewrite rules for more advice on how to debug this.

Resources