Mod rewrite rules for hosting multiple projects using Zend Framework - zend-framework

I have several small projects I want to host on single virtual host using Zend Framework. The structure is:
apps/
testapp1/
application/
index.php,
testapp2/
application/
index.php
Document root in virtual host looks like: DocumentRoot /var/www/apps/
I need univeral mod_rewrite rule which will transform URLs like /apps/testapp1/xxx into /apps/testapp1/index.php.
Any help will be greatly appreciated as I am trying to resolve that for several hours. Thank You.

If your DocumentRoot for website is /var/www/apps/, then I guess URLs would be http://www.example.com/testapp1/ajax instead of http://www.example.com/apps/testapp1/ajax. If so -- then you need to remove apps/ part from these rules.
These rules need to be placed in your .htaccess into website root folder.
If you already have some rules there then these new rules needs to be placed in correct place as order of rules matters.
You may need to add RewriteEngine On line there if you do not have such yet.
This rule will rewrite /apps/testapp1/ajax into /apps/testapp1/index.php, no other URLs will be affected:
RewriteRule ^apps/testapp1/ajax$ /apps/testapp1/index.php [NC,QSA,L]
This rule will rewrite ALL URLs that end with /ajax into /index.php (e.g. /apps/testapp1/ajax => /apps/testapp1/index.php as well as /apps/testapp2/ajax => /apps/testapp2/index.php):
RewriteRule ^(.+)/ajax$ /$1/index.php [NC,QSA,L]
UPDATE:
This "universal" rule will rewrite /apps/testapp1/xxx into /apps/testapp1/index.php as well as /apps/testapp2/something => /apps/testapp2/index.php:
RewriteRule ^(.+)/([^/\.]+)$ /$1/index.php [NC,QSA,L]

Related

301 redirect htaccess and Nginx settings issues and .composer folder

I've had some hit and miss results setting up htaccess to forward on a couple of aged domains I acquired to my main site. I wouldn't describe myself as advanced more an enthusiastic intermediate so a nudge in the right direction would be greatly appreciated. I've checked answers but nothing definitive that I can see that relates to my specific issue and the composer folder. In Apache/Nginx I have in directives (http/https)
RewriteEngine On
ErrorDocument 404 https://my-main-site.com/my-page-or-blog-post/
I have 'Restrict the ability to follow symbolic links' unchecked as I have reference to symlinks in htaccess and so avoids a 500 error. I also have put up a static html page so if anyone hits the old aged domain it's not an automatic redirect they'll see the static HTML page with reference to the old/aged domain. So the 301 is sort of in the background but I'm getting a 302 result. My htaccess looks like this......
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^old-site-aged-domain.com$ [NC]
RewriteRule ^(.*)$ https://my-main-site.com/my-page-or-blog-post/ [R=301,L]
However when I go in to AHREFS and look at the backlink data it's showing a 302, however it does confirm http>https is working fine.
http://www.old-site-aged-domain.com/info-page.html
301
https://www.old-site-aged-domain.com/info-page.html
302
https://my-main-site.com/my-page-or-blog-post/
In my site root I also have .composer folder (as well as error_docs, .ssh, httpdocs, logs, .revisium_antivirus_cache), I'm sort of aware what the purpose of this is (PHP) but there are 2 further htaccess files in there, the second smaller htaccess file only has 'deny from all' as content. I wonder if this folder with these 2 htaccess files is presenting a problem/conflict i.e. 3 x htaccess files. The smaller htaccess file is in .composer/cache. The larger htaccess file is in .composer. Should I delete these 2 x htaccess files in .composer folder.
Thanks in advance for any help
James
rewrote htaccess on numerous occasions but without success

How to change subdirectory to a subdomain in Apache2?

Lets assume I have a website abc.com with sub-directories x, y and z. So website urls are like,
abc.com/x/
abc.com/y/
abc.com/z/
Now lets assume I have 3 sub-domains for each sub-directory like below.
x.abc.com
y.abc.com
z.abc.com
Now I want to redirect requests like abc.com/x/ to x.abc.com. And I have lot of directories and I cannot add rules for each directory.
You can use dynamically configured mass virtual hosting (of mod_vhost_alias module). You need a wildcard entry in DNS server for resolution. For www.x.com.com and content on /home/x/www :
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteCond %{lowercase:%{HTTP_HOST}} ^www\.([^.]+)\.abc\.com$
RewriteRule ^(.*) /home/%1/www$1

Laravel RESTful Routes: Multiple apps with one backend 404

I am somewhat new to Laravel, and recently I got a setup at work to function exactly as I wanted. I have a single installation of Laravel with a subdirectory of apps that all use that same backend as a RESTful master.
The issue I'm having is that when I'm home, I've cloned this repo and changed the db settings to match my localhost at home (instead of at work). However, each time I try to load a rout for a resource I get 404'd!
Here's some code.
app/routes.php:
Route::get('/', function()
{
return 'fgh';
});
Route::any('config', function() {
return 'GET config';
});
//Main Rest Controller
Route::resource('main', 'MainController');
// Route::resource('config', 'ConfigController');
Route::resource('data', 'DataController');
The folder structure is:
|-- appREST (where Laravel is)
|-- shared
|-- apps (where the apps are)
What I am trying to do (which I do at work) is simply make an AJAX call from an app in the apps/ directory, but to a relative path that is adjusted for where appREST is.
At work, I AJAX to '../../../appREST/public/config', or ../public/main or whatever. However, for some reason, this fails at home. The same tables exist in both databases at work, and both are up and functioning (no one else has issues on either). It seems almost as if some local Apache or other setting on just my machine is failing?
I can load the first route for REST (Route::get('/')) in the browser, and 'fgh' IS displayed. However, even after changing to GET config and a string any call either AJAX or browser to any route but '/' 404s.
Also, as a note, in the meantime the applications themselves work fine. If I load static/hard data instead of what I'm trying to load, there are zero problems. This appears to be isolated to my routing in Laravel/Apache.
EDIT
I forgot to include the HTACCESS file for the /appREST/public folder. Here it is:
<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>
NOTE
The setup is a git repo and I actually have this on two separate machines. On the original machine, where the repo starrted as a working directory, the above code works fine. It is only on my home, clonedTo machine that the routes fail.
Does anyone have any ideas or suggestions?
Thank you!
The only way I eventually fixed this was a clean reinstall of both XAMPP and Laravel.
Given that I could find the '/' route and the errors were 404s, I'm going to tentatively call this an Apache error.
So, for anyone else having this issue: I suggest you check your XAMPP/web server settings if your Laravel routes 404!

subdomain redirect to subfolder using .htaccess and QNAP virtual host

I have DNS managed by NO-IP and self hosted websites on a QNAP, with Virtual hosts defined in the Qnap Software.
My sites are www.site1.com, www.site2.com and a new www.site3.com which (this one will work with subdomains created by wildcards at Users input in Joomla) (ex user1.site3.com would be the redirection of the site3.com/index.php/users/user1) and all are located in (root) web_folder/site1/...site2/...site3/ . The other sites have their virtual hosts working right.
Now, before doing that, i am getting stuck on this Qnap issue where i can't define my wildcard dns *.site3.com in the virtual hosts. I want to do this because when i enter in the browser anything.site3.com it gets me to the index.html file located in the root folder of my published sites (web/index.html)
What shoud I try in order for the subdomains to pass the root index and go directly to the website folder, where i could place my .htaccess containing the rewrite rules?
I tried with a .htaccess but couldn't manage what to write in order for it to work.
Define the wildcard in the qnap virtual host but does not accept
What am i thinking wrong?
I would like to avoid having a .htacces, in the web folder, to redirect every domain/subdomain to it's respective subfolder and afterwards having another .htaccess insinde my site3 folder which does the rest of the rewrites (subdomains wildcard and user based)
I have managed to do so far:
I shall partially answer to my question
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} !^www\.site3\.com<br>
RewriteCond %{HTTP_HOST} !^static\.site3\.com<br>
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)\.site3.com$ [NC]
RewriteRule ^(.*)$ http://site3.com/index.php/user/%2 [L]
The thing is, I would like to still see user.site3.com instead of the full link.
As an update. I am running wordpress multisite. My www.site3.com comes into the qnap(web or public_html folder) then the NAS virtual host manager redirects it to its actual subfolder (site3).
In this situation, when i write :
-anything.site3.com it reads my top (web or public_html) index and i can't get to the wordpress subpages. (my anything.site3.com page has been created from user-end)
-site3.com it goes to mi site3 folder as it should, managed by the qnap virtual host (i don't have to write additional htaccess.
So i need to somehow let my subdomains pass the redirection from the qnap and go to my site3 subfolder and then wordpress htaccess do it's job. This should be done so that i don't have a continuous loop!
Sorry for my explanations might be a little bit messed.

strange urls in Zend Framework

The Zend project creates URLs like:
http://localhost/zftest/public/index/add
How can I have a URL like
http://localhost/zftest/add.php
Thanks.
First off, you seem to be after getting rid of the "public" part. There are two ways to it.
First, the .htaccess. In the project's root directory, create two files: index.php and .htaccess, with the following contents:
.htaccess:
RewriteEngine On
RewriteRule .* index.php
index.php:
<?php
define('RUNNING_FROM_ROOT', true);
include 'public/index.php';
And there you have it, no "public" in the URL.
The other way to achieve it is by setting up a domain. You'll need to modify two files: hosts and httpd-vhosts.conf. The hosts file is located at /etc/hosts under Linux or in C:\Windows\System32\drivers\etc\hosts under Windows. Both need root level access. Just add the line
127.0.0.1 zftest.local
Then find your vhosts file. I'm using XAMPP, so here are the paths to it for XAMPP: C:\xampp\apache\conf\extra\httpd-vhosts.conf under Windows, /opt/lampp/etc/extra/httpd-vhosts.conf. Just add something like this:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /opt/lampp/htdocs/zftest/public
ServerName zftest.local
ServerAlias zftest.local
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_DOMAIN zftest.local
</VirtualHost>
Now, when you go to zftest.local (choose any name you wish, of course), you'll see your front page, no "public" part in the URL.
As for the other part of the question, you need a custom router. Setting up a router is really easy and you can find lots of information about on the internet. Some prefer to do it in the bootstrap, but I believe the easiest way is to do it in the application.ini file. Here's a sample router:
resources.router.routes.getbyroute.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.getbyroute.route = "(.+)\.php"
resources.router.routes.getbyroute.defaults.controller = index
resources.router.routes.getbyroute.defaults.action = index
resources.router.routes.getbyroute.map.1 = action
resources.router.routes.getbyroute.reverse = "%s.php"
This should take care of your route (in theory, I haven't tested it for correctness, hehe). Investigate routers though, as you'll probably want something more generic than this (this one won't handle other controllers than index).
I hope this helps.
I don't see the problem with the default Zend routes as that style of URL mapping is considered a good practice (SEO friendly, clear mapping from URLs to modules, controllers and actions), but if you want to change the routing you should read up on the Zend Controller Router Documentation. Then your mapping might e.g. look like this:
<?php
$router = $frontController->getRouter();
$router->addRoute(
'add',
new Zend_Controller_Router_Route('add.php', array('controller' => 'index', 'action' => 'add'))
);
?>
This is normally accomplished by using Virtual Host document root, or more commonly by dropping in an .htacess file to redirect all requests to your /public/index.php.
See http://framework.zend.com/wiki/display/ZFDEV/Configuring+Your+URL+Rewriter for more details.