httpd rewrite rule with a different DocumentRoot dir - mod-rewrite

Is there a way to make a rewrite rule to file located in a different than DocumentRoot path? Say I have domain http://test.ldt/ with DocumentRoot /home/test_ltd/ and I want that when a file is requested under static. subdomain (http://stats.test.ldt/) it would look for requested file from another path, say /home/static_files/
I was advised to use mod_alias. However, I am not sure how to make it work when I need it with subdomain.
to cristis:
You are not right. For example if these would be mine httpd rules:
ServerName domain.ltd
ServerAlias www.domain.ltd
DocumentRoot /home/domain_ltd
RewriteEngine on
RewriteCond %{HTTP_HOST} static2.domain.ltd
RewriteRule (.*)$ /home/static_files/$1 [L]
DirectoryIndex index.html index.htm index.php
And client would request for static2.domain.ltd/foo.txt, the file would be searched in /home/domain_ltd/home/static_files/$1

To me, it would make the most sense to just define the subdomain as a VirtualHost that has a DocumentRoot that points where you wanted it to (Although we're moving into ServerFault territory here technically, I guess...)
e.g.:
<VirtualHost *:80>
ServerName static.domain.tld
DocumentRoot /home/static_files/
<Directory /home/static_files>
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
But you could do it with a combination of mod_alias and mod_rewrite too, if you wanted. Something like this should work...
In httpd.conf:
Alias /static /home/static_files
In .htaccess (or preferably in httpd.conf in the Directory section for /home/domain_tld):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^static
RewriteRule ^!static - [C]
RewriteRule ^(.*)$ static/$1 [PT]

You can include the full path in the RewriteRule statement. For instance:
RewriteEngine on
RewriteRule ^/~(([a-z])[a-z0-9]+)(.*) /home/$2/$1/.www$3
(taken from http://httpd.apache.org/docs/2.0/misc/rewriteguide.html)

Related

Properly removing /public and connecting files

I've seen a lot of similiar topics and I tried all of them and previously it worked, every time different one, but now nothing...
So I want to remove /public from my URL and it works for laravel-public_html but not for build version
I'm using VPS (CentOS 7 with CWP 7)
My structure is like this:
/home/admin/
-laravel
-public_html (public folder for laravel)
-laravel_build
-build (public folder for laravel_build)
and with that I have build.mypage.com and www.mypage.com
httpd.conf
...
ServerRoot "/usr/local/apache"
...
DocumentRoot "/home/admin/public_html"
<Directory "/home/admin/public_html">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
index.php (public_html)
require __DIR__.'/../laravel/bootstrap/autoload.php';
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';
index.php (build)
require __DIR__.'/../laravel_build/bootstrap/autoload.php';
$app = require_once __DIR__.'/../laravel_build/bootstrap/app.php';
Should I add something in .htaccess , and if yes, where do I need to put that .htaccess?
Edit: I already have .htaccess in public folders for laravel routes(mod _rewrite)
Assuming you are using something like MAMP or XAMP, if possible do not add anything in httpd.comf, instead you could do something
like below in your httpd-vhosts.conf.
You may have to look for a directory in your environment where to put the virtual host file (and sometime you have to uncomment that path in your httpd.conf)
Example of entry in httpd-vhosts.conf:
<VirtualHost *:80>
ServerAdmin yourEmail#domain.com
DocumentRoot "/home/admin/laravel/public_html"
ServerName MySite1.com
ServerAlias MySite1.com
ErrorLog "logs/MySite1_com-error.log"
CustomLog "logs/MySite1_com_access.log" common
php_value error_log "logs/php_error.log"
</VirtualHost>
<VirtualHost *:80>
ServerAdmin yourEmail#domain.com
DocumentRoot "/home/admin/build"
ServerName subdomain.MySite1.com
ServerAlias subdomain.MySite1.com
ErrorLog "logs/subdoamain-MySite1-error.log"
CustomLog "logs/subdoamain-MySite1-error.log" common
php_value error_log "logs/php_error.log"
</VirtualHost>
Add two entries for both the apps.
I feel so stupid now, the problem was actually that I did not Hard Reset and Clear Cache in Google Chrome so it loaded previous configuration
For others that have similiar problem, solutions to try:
1.Create vHosts for subdomain and domain
2.Put .htaccess in public folder, not laravel folder
My current .htaccess is as follows(both public_html and build):
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
</IfModule>

Redirect /var/www/html/app/api/* to /var/www/html/api/*

I have a frontend app at /var/www/html/app and the API for that app is at /var/www/html/api. There is a virtual host set up for /var/www/html/app, with the name example.com. Now, I want it so what when a user visits example.com/api, it will redirect all requests to /var/www/html/api.
Example:
User enters http://example.com/api/some/call into their browser - instead of the server looking for /var/www/html/app/api/some/call, it will look for /var/www/html/api/some/call.
What is the best way to get this done? I tried using symbolic links which works for just /api, but not for variable routes after /api. I am thinking I can get it working with mod rewrite, but I am not that familiar with it.
UPDATE
Here is the config for example.com in /etc/apache2/sites-available:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/app
ServerName example.com
ServerAlias www.example.com
<Directory /var/www/html/app>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Here in the .htaccess file in /var/www/html/app (and the same one at /var/www/html/api):
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

mod_rewrite don't match on RewriteCond target - URL not found

I am trying to rewrite all URIs inside directory /docs to /docs/index.php, using apaches mod_rewrite module.
The URI doesn't exist as a directory structure, which I don't think is necessary when you are rewriting the URI to something that exit.
I am getting the error:
"The requested URL /docs/view/my_doc was not found on this server."
These are my rewrite rules:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^docs/([a-z]+)/([A-Za-z0-9_-]+)/?$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]
I am doing the configuration in httpd.conf. Also, I am using a multi v-host setup.
What's my problem and how do I fix it?
I'm using Apache/httpd on CentOS 6.2
UPDATE
Including my VirtualHost as well:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.smicloud.org
ServerAlias *.smicloud.org
VirtualDocumentRoot /var/www/html/www.smicloud.org
ExpiresActive On
ExpiresDefault "access plus 14 days"
# insert logging config, anything else you need..
#Define FileEtag so it doesnt use node
FileETag MTime Size
<Directory /var/www/html/>
Order Allow,Deny
Allow from all
# Get rid of this if you need to allow htaccess files:
AllowOverride None
</Directory>
RewriteEngine On
RewriteRule ^docs/([a-z]+)/([A-Za-z0-9_-]+)/$ /docs/index.php?action=$1&target=$2 [QSA,L]
</VirtualHost>
This should work
RewriteEngine On
RewriteRule ^docs/([a-z]+)/([A-Za-z0-9_-]+)/$ /docs/index.php?action=$1&target=$2 [QSA,L]
Ok, there were a couple of things that I had to do in order to fix it, using #donalds suggestion (which I actually tried before):
1 - I had to have a '?' at the end of the regex of RewriteRule:
^/docs/([a-z]+)/([A-Za-z0-9_-]+)/?$
Then I can chose whether to have a '/' or not at the end of the address.
2 - I had to add a '/' to the beginning of the regex of my RewriteRule. I.e.:
^/docs/([a-z]+)/([A-Za-z0-9_-]+)$
3 - I also had to add the 'http://%{HTTP_HOST}/' part to the new URI:
http://%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]
This is what I ended up with:
RewriteRule ^/docs/([a-z]+)/([A-Za-z0-9_-]+)$
http://%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]
Perhaps why I needed to add the %{HTTP:HOST} was because it's a multi virtual host setup.

Mod_rewrite doesn't seem to work for this rewrite rule

I'm trying to set up a simple rewrite rule so that something like
mysite.com/admin
becomes
mysite.com/new/admin
I have tried the following :
RewriteEngine On
RewriteRule ^admin.* new/admin [L]
But that doesn't seem to be working. It just results in a 404 error, which is the same as what I was getting before. My logs don't seem to show anything. Here is my configuration file:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName mysite.com
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
If anyone knows the proper rewrite rule to settle this, that would be most excellent!
Try this:
RewriteEngine On
RewriteBase /
RewriteRule ^admin(.*) new/admin$1 [L,R,QSA]
Flags explanation:
L - last rule (processing will stop at that point)
R - redirect (without this flag, apache will do sub-request and will still show the original URL to the browser)
QSA - append the original query-string
Can you please try the following with the slash..
RewriteRule ^admin.* /new/admin [L]

vHost RewriteRule is creating a 500 Error

Below you will find my current vHost entry that I am using for a site that I currently have under development. This vHost entry works fine when I have it on my local machine, but when I push my code to my staging server that is running this same vHost record I receive a 500 Internal Server error.
The machine I'm running this vHost on is running Apache 2.2.9 (Debian).
<VirtualHost 206.217.196.61:80>
SuExecUserGroup 13labs 13labs
ServerAdmin aellis#1three.com
ServerName admin.13labs.net
ServerAlias admin.13labs.net
DirectoryIndex index.php
DocumentRoot /var/www/13labs.net/html/admin/
ErrorLog /var/www/13labs.net/logs/error.log
# Hide .svn Directories
<DirectoryMatch "\.svn">
Order deny,allow
deny from all
</DirectoryMatch>
# FastCGI
Alias /fcgi-bin/ /var/www/13labs.net/fcgi-bin/
AddHandler php-fastcgi .php
AddType application/x-httpd-php .php
Action php-fastcgi /fcgi-bin/admin-php.fcgi
<Directory /var/www/13labs.net/fcgi-bin/>
SetHandler fcgid-script
AllowOverride None
Options -Indexes +ExecCGI -FollowSymlinks -SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/13labs.net/html/admin/>
AllowOverride None
Options -Indexes -FollowSymlinks -SymLinksIfOwnerMatch
FileETag All
</Directory>
# Rewrite Logic
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/(.+)$ /index.php/$1 [PT,QSA,L]
Thanks for any help that you can provide.
Best regards,
Andrew
After many trial and errors I have found that the working RewriteRule needs to be the following:
RewriteRule ^.*$ /index.php$1 [PT,QSA,L]
Then in PHP I need to be using $_SERVER['REQUEST_URI'] instead of $_SERVER['PATH_INFO'] to make sure my PHP script see the passed in URI.

Resources