php links not working in sub folder of multi site - php

Testing two identical folders , one placed in a Drupal install folder and the other in a plain html subfolder in
home root , only the php links for the one in CMS install work. This makes sense because Drupal has a settings php
file and .htaccess file which makes sure everything is in its proper place for links to work whether in home root or not. However I thought it would be easy enough to get the links to work in plain html subfolder with a simple rewrite
rule in the .htaccess file which exists in the php folder. Yet try as I might, nothing has worked so far.
The configuration in /etc/httpd/conf.d/my.conf
<VirtualHost *:80>
ServerName drupalsite.com
ServerAlias www.drupalsite.com
ServerAdmin vps#drupalsite.com
DocumentRoot "/var/www/html/drupalsite.com"
<Directory /var/www/html/drupalsite.com>
AllowOverride All
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName plainhtml.com
ServerAlias www.plainhtml.com
ServerAdmin vps#plainhtml.com
VirtualDocumentRoot "/var/www/html/plain”
</VirtualHost>
The .htaccess file in identical php folders
RewriteEngine On
RewriteBase /phpfolder/
RewriteRule ^c-(.*)$ catpost.php?id=$1 [L]
RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
So drupalsite.com/phpfolder links all work
But plainhtml.com/phpfolder links do not work
phpfolder for Drupal resides in /var/www/html and Drupal site is symlinked
phpfolder for plain html subfolder to root resides in /var/www/html/plain
Both phpfolders are owned by apache:root and all files within are owned by root:root
Have also tried changing the .htaccess rewrite rule to:
RewriteBase /
RewriteBase /plain/
RewriteBase /plain/phpfolder/

You will have to define a Directory entry with AllowOverride for:
/var/www/html/plain
<Directory /var/www/html/plain>
AllowOverrideAll
Allow from all
</Directory>
Note: If you have access to the main configuration file of the server you shouldn't be using .htaccess, configuring Rewrites or Redirects in Virtualhost is simpler. As you have already seen, using sub-files and per-dir configurations make things more complicated, not counting the overhead of apache having to constantly check those files when AllowOverride is enabled.

Related

Redirecting to Index file in virtual host with htaccess when request file not found

I've set virtual host in my xampp to access my sites.
I created a folder my-project in htdocs folder and set virtual host as the following,
I edited hosts file in windows\system32\drivers\etc folder and added this line:
127.0.0.1 my-project.com www.my-project.com
After that, I edited file httpd-vhosts in D:\xampp\apache\conf\extra folder and added following code.
<VirtualHost *:80>
DocumentRoot "D:/xampp/htdocs/my-project"
ServerName my-project.com
ServerAlias www.my-project.com
</VirtualHost>
Now, I can access my project with the uri my-project.com/public and index file is loaded, but the problem comes in .htaccess file which I've created under public folder (same folder which contains index.php file) with following lines.
<IfModule mode_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /my-project/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
Now problem is that if I type my-project/public/somefile.php and file does not exist then it is not redirecting to index.php file under public folder.
Could anyone please advise as to the solution ?
DocumentRoot "D:/xampp/htdocs/my-project"
:
RewriteBase /my-project/public
If the DocumentRoot is set to the /my-project directory, then the RewriteBase directive should only reference /public (it is relative to the document root), not /my-project/public (my-project.com/my-project/public/index.php is presumably not the intended URL?).
In other words:
RewriteBase /public
Or, remove the RewriteBase directive completely, as it's not required if the .htaccess file is actually in the /public subdirectory.

htaccess invisibly rewrite folder

I have multiple-domains with different content on one server in the directory /html/, which serves all domains, like example1.com, example2.com, example3.com, example4.com.
This works fine.
I now need to have 2 different(!) wordpress-installations. From the outside I would like to have:
example1.com -> No Blog
example2.com/blog/
example3.com -> No Blog
example4.com/blog/
The internal path-structure on the server should look like:
/html/
/blog-for-server2/
/blog-for-server4/
Because otherwise the content-management-system in /html/ gets messed up. In fact this is almost like having independent subdomains for the blogs and pointing them to different pathes on the server (but I don't want to use subdomains).
Any ideas?
Assuming you've already created the domains in Apache's httpd-vhosts.conf file you can create the redirects inside that, something like:
<VirtualHost *:80>
ServerName example1.com
DocumentRoot /home/servers/common_root/html
RewriteEngine On
RewriteRule ^/blog/? /blog-for-server-1
</VirtualHost>
<VirtualHost *:80>
ServerName example2.com
DocumentRoot /home/servers/common_root/html
RewriteEngine On
RewriteRule ^/blog/? /blog-for-server-2
</VirtualHost>
Otherwise you can do it in a root .htaccess but it's messy:
RewriteEngine On
RewriteCond %{SERVER_NAME} =example1.com
RewriteRule ^blog /blog-for-server-1
RewriteCond %{SERVER_NAME} =example2.com
RewriteRule ^blog /blog-for-server-2

Redirecting a subdomain with .htaccess

I have a subdomain set up on my hosting: indiantimes.indianradio.net.au, that is being pulled from a folder in my /public_html folder: /public_html/indiantimes.com.au.
I am trying to write an .htaccess rule that will redirect it to that folder still, but retain the original url the user typed in: indiantimes.indianradio.net.au.
I have only been able to get the redirect working, i.e. (indiantimes.indianradio.net.au redirects to indianradio.net.au/indiantimes.com.au/), but I can't seem to get the redirect working so the url seen by the user, stays at: indiantimes.indianradio.net.au. The majority of the image urls are broken intil I am able to get the redirect working properly.
The .htaccess rule I was playing around with was:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^indiantimes\.indianradio\.net\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.indiantimes\.indianradio\.net\.au$
RewriteRule ^/?$ "http\:\/\/indianradio\.net\.au\/public_html\/indiantimes\.com\.au" [R=301,L]
What am I doing wrong with the redirect? Any help would be much appreciated! Thanks in advance!
You have to replace your sub folder name to be the same as your sub domain
(indiantimes.com.au -> indiantimes).
RewriteEngine On
RewriteCond %{HTTP_HOST} ^indiantimes\.indianradio\.net\.au$
RewriteCond %{REQUEST_URI} !^/indiantimes/
RewriteRule (.*) /indiantimes/$1
source
for subdomains it is generally recommended to add a virtualhost in apache instead of using .htaccess (preformance-wise and more cross-platform).
However you might find the following link suitable in case editing the apache config files isn't an option: .htaccess rewrite subdomain to directory (summary: using mod-proxy and adding the P flag to your RewriteRule)
Go to /etc/apache2/sites-available (use cd in terminal)
Add a new file named: indiantimes.indianradio.net.au, example content:
<VirtualHost *>
DocumentRoot /var/www/indianradio.net.au/public_html/indiantimes.com.au/
ServerName indiantimes.indianradio.net.au
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews +Includes
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error-logfile.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access-logfile.log combined
</VirtualHost>
link to the file in apache2/sites-enabled
in terminal: ln -s ./indiantimes.indianradio.net.au ../sites-enabled/ from the sites-available folder, note the trailing /!

Laravel 4 - 403 Permissions error when trying to access css/js files

I'm running on an Ubuntu 13.10 dev environment with Apache2 and after hours of trying to figure out permission errors to access routes, I was able to fix it. Now I can successfully browse through my application, but the problem that now exists is that I cannot access my css/js files within my public directory - it kicks back with a 403.
I've tried modifying the .htaccess file, the virtual host config file, and ran chmod on the entire site directory.
Here's a copy of my .htaccess file within the Public folder:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
And here's a copy of my virtual host file:
<VirtualHost *:80>
DocumentRoot "/home/casey/Sites/caseyhoffmann.me/public"
ServerName caseyhoffmann.me.dev
<Directory "/home/casey/Sites/caseyhoffmann.me/public/">
AllowOverride all
Require all granted
DirectoryIndex index.php index.html
Options -MultiViews
</Directory>
</VirtualHost>
Any ideas?
Solved. Reapplied chown and redid chmod on the directory. File system permissions issue and had nothing to do with Apache.
Solution 1: (Recommended)
use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
Solution 2:
You can try adding this in your page's header:
<base href="/" />

Htaccess Rewrite to PHP File in Parent Directory

I use a custom framework which has many sub applications. I want to assign a domain to each application folder but ran into an issue.
Apache vhosts config
<VirtualHost *:80>
ServerName app1.com
DocumentRoot /var/www/framework/public/app1
</VirtualHost>
<VirtualHost *:80>
ServerName app2.com
DocumentRoot /var/www/framework/public/app2
</VirtualHost>
/var/www/framework/.htaccess
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ index.php?_ROUTE=$1 [QSA]
/var/www/framework/index.php
<?php
exit('WORKED!');
//Route request
//...
Everything's works fine except that it tries to use the index.php file in the document root such as "/var/www/framework/public/app1/index.php" when actually I want to use the index file "/var/www/framework/index.php".
So how would I get it to use the index.php file two directories above or relative to the .htaccess location?
You don't. The .htaccess will only be read starting at the root folder of the virtualhost.
You either need to clone the file (ugly unless you're using something like a source code repository to pull it as an external) or add it to your vhosts config. I'd recommend just making one 'rewrite.conf' file and loading it through the vhosts config through include.
vhosts conf:
<VirtualHost *:80>
ServerName app1.com
DocumentRoot /var/www/framework/public/app1
Include rewrite.conf
</VirtualHost>
<VirtualHost *:80>
ServerName app2.com
DocumentRoot /var/www/framework/public/app2
Include rewrite.conf
</VirtualHost>
rewrite.conf:
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ index.php?_ROUTE=$1 [QSA]
This does have a few drawbacks:
You'll need to reset the server every time you change it.
If you have a lot of rewrites, you'd probably want to keep this somewhere at the code level... however, I'd argue that you probably want a different solution (as in, a PHP handler) if you have a whole mess of rewrites.
Note that you could also just do the same rewrite directly in the .htaccess, but if you're going to do it, lower level seems better.
What you're doing is essentially a multi-site application.
You should set the document root to the folder that contains the main index.php file. Then that should route the request to the required app by checking the HTTP host property in the $_SERVER superglobal, like for example:
switch ($_SERVER['HTTP_HOST']) {
case 'app1.com':
include 'app1/index.php';
// Do stuff
break;
case 'app2.com':
include 'app2/index.php';
// Do stuff
break;
}

Categories