RewriteRule to cause subdomain to load another DocumentRoot? - apache

Apache (mostly) noob here... could sure use a little help with what I am trying to do with a RewriteRule in .htaccess:
...to causes URLs using our sharpedge. subdomain to auto-load from the Internet_IE directory - one level deeper than the site root.
I have this in httpd.conf:
[snip]
NameVirtualHost 11.22.33.44
<VirtualHost 11.22.33.44>
Options All +ExecCGI
ServerAdmin hostmaster#ourhost.com
DocumentRoot /var/www/html/ourdomain.com
ServerName ourdomain.com
ServerAlias www.ourdomain.com
DirectoryIndex index.htm index.html index.dna
#---------------------------------
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
#---------------------------------
</VirtualHost>
<VirtualHost 11.22.33.44>
Options All +ExecCGI
ServerAdmin hostmaster#ourhost.com
DocumentRoot /var/www/html/ourdomain.com
ServerName sharpedge.ourdomain.com
DirectoryIndex index.htm index.html index.dna
#---------------------------------
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
#---------------------------------
</VirtualHost>
[snip]
...and this in .htaccess (in the site root, here: /var/www/html/ourdomain.com/)
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^sharpedge\.ourdomain\.com$
# years ago this next line was used here, but in httpd.conf: (I don't know what it was supposed to do)
# RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^sharpedge\.ourdomain\.com(.*) /var/www/html/ourdomain.com/Internet_IE/$1 [L]
</IfModule>
..but nothing happens from the RewriteRule; it is as if the RewriteRule was not there.
I'd much appreciate any suggestions.
Edits:
I would just change the DocumentRoot to the directory I actually want to serve, but I need to be able to call (include) files from within files in that directory with a prefixing / in the include path, where those files are located in the site root. Those files are also similarly called from other files that are in the site root.

You don't want to put any part of your host in the RewriteRule, but you got the HTTP_HOST right, try:
RewriteCond %{HTTP_HOST} ^sharpedge\.ourdomain\.com$ [NC]
# to prevent looping
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Internet_IE/$1 [L]
Try changing the !-f and !-d checks:
RewriteCond %{HTTP_HOST} ^sharpedge\.ourdomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/Internet_IE/
RewriteRule ^(.*)$ /Internet_IE/$1 [L]

Related

Apache vhost double slash on redirects

I was trying to force the FQDN and HTTPS via Apache Vhost redirects in a Laravel application behind a reverse Proxy. My problem now is that the user sometimes gets redirected to a URL with double slash e.g. https://exampledomain.xy//test instead of https://exampledomain.xy/test and I'm not sure why.
Here is my apache vhost configuration:
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/public
<Directory /var/www/html>
AllowOverride All
</Directory>
ErrorLog /dev/stderr
TransferLog /dev/stdout
RewriteEngine on
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=::1
RewriteRule (.*) https://exampledomain.xy/$1 [L,R=301]
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot "/var/www/html/public"
ServerName exampledomain.xy
</VirtualHost>
And my .htaccess file (default .htacces file that comes with Laravel)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Okay looks like I fixed the issue. I changed the following in my config:
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://stk-support.ukl.uni-freiburg.de$1 [R=301,L]
The extra slash in the rewrite rule was causing the issue and I also added a HTTPS rewrite condition.

htaccess mod rewrite and slash after index.php

I have following .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
but perhaps because of apache update to version 2.4.23 this one stopped working and browser returns message file not found.
I found out, that URL
example.com/auth/login
searches for index.php under {document_root}/auth/login directory and not directly in {document_root} and therefore it's not working. Putting directly %{DOCUMENT_ROOR} variable before index.php doesn't work, it looks like apache thinks, that document root is the one taken from URL.
How can I rewrite it to index.php with whole path put after index.php?
EDIT
I'm using fcgi and this is my vhost file:
<Directory "/Users/pogo/Development/www">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
<VirtualHost *:8080>
ServerName dh
ServerAlias *.dh
VirtualDocumentRoot "/Users/pogo/Development/www/%-2.0.%-1/%0/www"
DirectoryIndex index.html index.php
RewriteEngine On
<FilesMatch \.php$>
SetHandler "proxy:unix:/usr/local/var/run/php5/php5-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
You can use this rule:
AcceptPathInfo On
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]
Notice /index.php instead of ./index.php

Delegate specific URL request to specific Directory

Taking forward from my last question i.e. Mapping domains to Tomcat Apps. Now I want that if a particular request came i.e.
http://www.app1.com/blog
then this would be redirected to a specific folder on my server. I did tried below code
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/blog/ /path_to_my_site/app1_com/blog/ [L]
but that has gone horribly wrong as that has become redirect loop, so I removed it immediately.
Note: My site www.app1.com is hosting java application and www.app1.com/blog would be php application blog.
Update 1
for the sake of clarity below is my virtual host configuration
<virtualhost *:80>
ServerName app1.com
ServerAlias app1.com www.app1.com
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^blog(/.*)?$ /path_to_my_site/blog/ [L,NC]
ProxyPass / ajp://127.0.0.1:8009/app1/
ProxyPassReverse / ajp://127.0.0.1:8009/app1/
</virtualhost>
Update 2
contents of .htaccess in blog folder is
# Pretty Permalinks
RewriteRule ^(images)($|/) - [L]
RewriteCond %{REQUEST_URI} !^action=logout [NC]
RewriteCond %{REQUEST_URI} !^action=login [NC]
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /blog/
RewriteCond %{REQUEST_URI} !index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?filename=$1 [NC,QSA,L]
You can use:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^blog(/.*)?$ /path_to_my_site/app1_com/blog/ [L,NC]
/path_to_my_site/app1_com/blog/ should be a valid path under DocumentRoot.
Change your ProxyPass line to this:
ProxyPass /blog !
ProxyPassMatch ^/(?!blog).*$ ajp://127.0.0.1:8009/app1/
ProxyPassReverse / ajp://127.0.0.1:8009/app1/

How can I convert my virtual hosts modrewrite in httpd.conf to use .htaccess instead?

I am moving to a new host that does not allow access to edit httpd.conf or vhosts.conf, so I need to use .htaccess instead.
Here's my existing virtual host conf:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.dev
DocumentRoot /path/to/html
Alias /sitemap.xml /path/to/html/sitemap.php
Alias /sitemap.xml.gz /path/to/html/sitemap.php
AliasMatch ^/sitemap(.*).xml /path/to/html/sitemap.php
AliasMatch ^/sitemap(.*).xml.gz /path/to/html/sitemap.php
Alias /robots.txt /path/to/html/robots.php
AliasMatch ^/robots.txt /path/to/html/robots.php
<Directory /path/to/html>
AllowOverride none
Options all
Order allow,deny
Allow from all
Deny from none
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
# The following redirects all directories to root using PATH variables
# Ex. /abc/def/ redirects to /index.php/abc/def/
RewriteCond %{REQUEST_URI} !=/server-status
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1 [L]
</IfModule>
</Directory>
</VirtualHost>
The goal is to get the same interaction using htaccess.
The primary interaction I'm looking for is:
sitemap.xml => sitemap.php
sitemap-abc.xml => sitemap.php
sitemap-___.xml => sitemap.php
sitemap-xyz.xml => sitemap.php
robots.txt => robots.php
/abc/def/ => /index.php/abc/def/
The above vhosts.conf works just fine on my old host, but I'm unsure how to execute this on my new host with only htaccess.
Turns out my specific issue was with a hosting configuration instead of a mod_rewrite problem. But for others, here's my solution:
/path/to/html/.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

How go to home page correctly?

I enable mod_rewrite but when I go to home page the browser try download something. When I go to app_dev.php the browser show error:
The requested URL /app_dev.php/ was not found on this server.
My locale host was configurated to web directory.
the output of 'app/console router:debug _welcome' is:
[router] Route "_welcome"
Name _welcome
Path /
Host ANY
Scheme ANY
Method ANY
Class Symfony\Component\Routing\Route
Defaults _controller: Acme\DemoBundle\Controller\WelcomeController::indexAction
Requirements NO CUSTOM
Options compiler_class: Symfony\Component\Routing\RouteCompiler
Path-Regex #^/$#s
my httpd-vhosts.conf:
<VirtualHost *:80>
ServerAdmin myemail#email.com
DocumentRoot "/var/www/symfony/hz.dev/web"
ServerName hz.dev
ServerAlias www.hz.dev
ErrorLog "/var/log/apache2/hz.dev-error_log"
CustomLog "/var/log/apache2/hz.dev-access_log" common
</VirtualHost>
My web/.htaccsess
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule .? %{ENV:BASE}app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
Try this .htaccess instead. It's very simple, I use it during development.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
</IfModule>
You should then be able to access Symfony at http://example.com/ without app_dev.php in the URL