Mod_rewrite without {%Document_Root} - apache

I used to point all my domains to my web-spaces root. And use an htaccess file like this to point the different domains into different folders
RewriteCond %{HTTP_HOST} ^new.vea.?re.?[net|de]?(.*)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/veare/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/veare/$1 -d
RewriteRule ^(.*)$ veare/$1 [L]
# rewrite parameters that are neither files nor folders
RewriteCond %{HTTP_HOST} ^new.vea.?re.?[net|de]?(.*)?$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ veare/index.php?/$1 [L]
My problem now is, that my new web provider does not set the %{DOCUMENT_ROOT} so this script doesn't work. Is there any workaround?
So its like this.
www.test.com/css/style.css
1. Domain points to root.
2. If root/test.com/css/style.css exists, it needs to just grab this file, aka redirect into the subfolder test.com
3. If it is neither a file, nor a folder, it should just point to the index.php in the subfolder root/test.com/index.php
How can i solve it WITHOUT %{DOCUMENT_ROOT}?

Okay,
easy fix, use the <?php phpinfo(); ?> which should indeed tell you the real document root. eg. customer/123445_12345. Now you can just use this in your htaccess. Note, that I also had to use %{REQUEST_URI} instead of %{REQUEST_FILENAME} to get the full path, like /css/app.css
RewriteCond %{HTTP_HOST} ^test.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond /customer/123445_12345/test/public/%{REQUEST_URI} !-f
RewriteCond /customer/123445_12345/test/public/%{REQUEST_URI} !-d
RewriteRule ^(.*)$ /formandsystem/cms/test/index.php?/%{REQUEST_URI} [L]
RewriteCond /customer/123445_12345/test/public/%{REQUEST_URI} -f [OR]
RewriteCond /customer/123445_12345/test/public/%{REQUEST_URI} -d
RewriteRule ^(.*)$ /test/public/%{REQUEST_URI} [L]

Related

.htaccess check if rewrite subdirectory exists

I want to point each subdomain to a corresponding directory by default. To make things simple subdomain and directoryname are the same. This works well but is there any way to check if the actual directory exists? If not I don't want to do the rewrite.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$
RewriteCond %{REQUEST_URI} !^/%1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$
RewriteRule ^(/)?$ %1/index.php [L]
You may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteCond %{DOCUMENT_ROOT}/%1/index.php -f
RewriteRule ^ %1/index.php [L]
This will rewrite to a subdomain.example.com/foobar to subdomain.example.com/subdomain/index.php only if subdomain directory exists with a index.php file in it.

Redirect WWW to one subfolder and wildcard to another htaccess

I am looking to create work this Htaccess in a way that allows me to send www. to one subfolder and and any wildcard subdomains to another folder on a CentOS7 Linux server. The file structure is as follows as both subfolders have different php frameworks within them: -
.htaccess
cms (Yii)
app (Symfony)
The Htaccess File I currently have (it's been chopped around a bit) currently looks like this: -
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/cms/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /cms/public/index.php?p=$1 [QSA]
RewriteCond %{HTTP_HOST} ^(*.)?example.com$
RewriteCond %{REQUEST_URI} !^/app/web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/web/$1
RewriteCond %{HTTP_HOST} ^(*.)?example.com$
RewriteRule ^(/)?$ app/web/$1 [QSA,L]
Any help of making this work would be really appreciated.

htaccess point request to sub-directory if file doesn't exists and does under the subdirectory

I'm having a really hard time with trying to get this to work so I have a sub-directory example.com/projects with a directory under that called test and I'd like it if someone typed example.com/test it would show the file from example.com/projects/test but if the file does not exist I would like it to post a 404 error.
Additionally if possible I would like any time example.com/projects/test was typed it would redirect to example.com/test instead but if someone goes to just projects/ I want them to be able to stay there.
I have tried the following .htaccess files so far
This works almost perfectly passing through the files unless the file doesn't exist in which it throws a 500 Internal Server Error
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ projects/$1 [PT]
I thought something like this might work to only pass through if the file existed under projects but doesn't work at all:
RewriteCond projects/%{REQUEST_FILENAME} -f
RewriteCond projects/%{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ projects/$1 [PT]
For the second part pointing anything that goes to /projects/test to strip /projects I tried this (placed before the code above)
RedirectMatch ^/projects/(.+)$ /$1
Only to find a redirect loop. How can i prevent this?
My current .htaccess file for reference:
ErrorDocument 404 /404.html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example2\.com$
RewriteRule ^/?$ "http\:\/\/example3\.org" [R=301,L]
#RedirectMatch ^/projects/(.+)$ /$1
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ projects/$1 [PT]
You can use these rules in your root .htaccess:
DirectoryIndex index.html
RewriteEngine On
RewriteCond %{THE_REQUEST} /projects/(\S+)\sH [NC]
RewriteRule ^ /%1 [L,R=302,NE]
RewriteCond %{DOCUMENT_ROOT}/projects/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/projects/$1 -d
RewriteRule ^(.+)$ projects/$1 [L]

redirect with .htaccess rewrite rule to different folder on my server

I have been trying to redirect to a different folder with .htaccess when I hit a domain.
Here's my .htaccess redirect rule, can you tell me where am I wrong?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
So, what I want is, if someone visits racereadymotorsports.in, they redirect to /raceready/ directory
and on the other hand if they visit to annelies-slabbynck.com, they should redirect to /annelies/ directory
At present, this redirects twice for annelies-slabbynck.com which means that, I end up with:
http://www.annelies-slabbynck.com/annelies/annelies/ as my final url
I am running a shared hosting and do not have access to add a new configuration.
Please help.
You need to remove the [OR] flag and reproduce the same conditions with the other rule. Maybe something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
Or better yet, you can use these conditions instead: RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteCond %{DOCUMENT_ROOT}/raceready/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/raceready/$1 -d
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteCond %{DOCUMENT_ROOT}/annelies/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/annelies/$1 -d
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
Note that the R=301 flag in your second rule redirects the browser, unlike the first rule which only internally rewrites the request.

How can I disable RewriteRule for one subcategory?

I have a .htaccess in my root of website that looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.pl [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([a-z0-9_-]+)\.mydomain\.pl [NC]
RewriteRule ^/?$ /index.php?run=places/%1 [L,QSA]
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/upload/
RewriteCond %{REQUEST_URI} !^/javascript/
RewriteRule ^(.*)$ /index.php?runit=$1 [L,QSA]
But when I type:
mydomain.pl/guests
I would like to go normally to actual folder guests. I understand that I need to somehow disable this rule for guests subfolder, but how do I do this?
EDIT:
I've included whole .htaccess file
I've put a htaccess in subfolder and added RewriteEngine Off and it works.
Add one of these
RewriteEngine On
#If the request starts with guests, then pass it through
RewriteCond %{REQUEST_URI} ^guests
RewriteRule ^ - [L,QSA]
#Or, if the request contains an existing filename or directory, pass it through
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L,QSA]