Zend Framework URL Rewriting Using Htaccess - zend-framework

Guys,
I want to write a .htaccess rewrite rule in zend framwork.
MY URL is http://www.somedomain.com:8090/hotel-profile
I want to redirect this URL to http://www.somedomain.com:8090/disabled
So to achieve this goal I have added following to my existing .htaccess file.
RewriteRule ^hotel-profile$ /disabled
But it is not working for me.
my .htaccess files looks like:
RewriteEngine On
RewriteBase /
RewriteRule ^hotel-profile$ /disabled
# Activate Zend Framework.
RewriteCond %{SCRIPT_FILENAME} !-s
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
Thanks.

Why don't you use a plugin which use a predispatch method? This could be usefull if you want to add more redirects like this.

Related

How to remove "public" from url using routeing in zend framework

An one issue in my zend, i write rule in .htaccess to remove "public" from url as following,
------------------------------------------------------------------------
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain-name.com/$ [OR]<br/>
ReWriteCond %{REQUEST_URI} !public/<br/>
ReWriteRule ^(.*)$ public/$1 [L]<br/>
------------------------------------------------------------------------
but there is ROUTE method in zend, i have use it for multiple language to set language code in url LIKE www.domain-name.com/en/ , using zend_controller_router_route_chain,
before this method implemented, my url is www.domain-name.com but
when i use this method in my zend project, may be it overwrites .htaccess rule of removing "public" from URL or something happen using same and "public" is displaying in url like www.domain-name.com/public.
so IS THERE ANY METHOD OR TRICK TO REMOVE PUBLIC FROM URL USING ANY METHOD OF ROUTE IN ZEND FRAMEWORK ???
Thanks,
MRJethva.
The following works for me using ZF2.2
Create index.php on ZF2 root directory and add the following content:
<?php
define('RUNNING_FROM_ROOT', true);
include 'public/index.php';
Create .htaccess on ZF2 root directory and add the following content:
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteRule .* index.php
Finally, add this conditional statement in the top of your layout.phtml file:
<?php
if (defined('RUNNING_FROM_ROOT')) {
$this->plugin('basePath')->setBasePath($this->basePath().'/public');
} ?>
Enjoy!
Reference: http://akrabat.com/zend-framework/zend-framework-on-a-shared-host/
Yes, and you don't need Zend_Route. Delete the public folder and put the Zend Framework files from it (index.php, .htaccess, etc) in your root directory (e.g. htdocs). You can place the application folder and other Zend Framework files outside of your web root where they cannot be accessed over HTTP.
All you need to do is edit index.php and change the APPLICATION_PATH to the correct path. This way your Zend Application will run from your root directory and you won't need to use mod_rewrite to hide the public folder.
See the last part of this answer for a similar example.
zf2 and zf3 remove Public from URL
create index.php and .htaccess add file in root of project
add the line in index.php
include_once("public/index.php");
and in .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
after that go to following path
Module->application(module name)->view->layout->layout.phtml
change css and js path add public in path
before
->prependStylesheet($this->basePath('css/bootstrap.min.css'))
->prependFile($this->basePath('js/bootstrap.min.js'))
after
->prependStylesheet($this->basePath('public/css/bootstrap.min.css'))
->prependFile($this->basePath('public/js/bootstrap.min.js'))
also in image path

Zend Framework - do not treat /public/directory as a controller

Is it possible to request a directory over http in the Zend /public/ directory without it being treated as a controller?
Eg, I have a directory, /public/facebook/ that contains facebook app data but Zend is (as default expected behaviour) giving "facebook controller missing" error when I request http://mysite.com/facebook.
This is because the default RewriteRules provided with Zend are redirecting all requests to the Zend Framework Bootstrapper (index.php).
The best thing to do, IMHO, is to add an exception to this Rule. Then there's more than one way to do it. You could use RewriteCond (and for example avoid the general redirect for existing directory or files in /public). But you could maybe filter it in the RewriteRule expression as well. This is an example with exception in the RewritreRule, so withoutt RewriteCond:
RewriteRule !(public/facebook/(.*\.php))|(public/(.*\.pdf))|(externalauthent\.php)|(favicon.ico)|(images/(.*\.(ico|gif|jpg|png|bmp|JPG|JPEG|BMP|GIF|PNG)))|(css/(.*\.css))|(js/(.*\.*))$ /index.php [L]
If you have content in this url, ZF don't try to found facebook controller, You need add a folder named facebook, and a file index.php into this folder
This work for me:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI} !^/public_directory_name
RewriteRule ^.*$ index.php [NC,L]
Just replace 'public_directory_name' with the name of the directory you don't want Zend to treat as a controller.
Answered in a different worded question: htaccess - do not rewrite in this case

Zend and modrewrite not working together properly

I'm setting up a Zend application, and went through the basic steps of setting up modrewrite on my development station, and writing an .htaccess file (shown below). Now, when I go to the root of my project, Zend works properly, calling the controller and displaying the appropriate page.
However, when I call any controller, I would expect it to redirect to the same index.php file, which would in turn direct it to the appropriate controller and action (e.g. myurl/controller/action would be read by myurl/index.php which would then redirect to the action appropriately).
I suspect that the issue is somehow related to how I've set up my .htaccess file, since calling the base url does work properly. But I haven't been able to figure out what's wrong. Does anyone have a suggestion?
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)/(.*)$ index.php [NC,L]
RewriteRule ^(.*)/(.*)/(.*)$ index.php [NC,L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ index.php [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Okay, here's what it was.
My DOCUMENT_ROOT is set to /var/www while my application is running out of /var/www/app1 with everything set up in there. Because of the line RewriteBase / it was redirecting to the DOCUMENT_ROOT instead of to the APPLICATION_ROOT. I fixed it by changing that line to:
RewriteBase /app1
and now everything works perfectly.

Zend Framework in a subfolder

I have installed a Zend Framework application in a subfolder, something like this:
www.mydomain.com/temp/zend-application/
Now the problem is all stylesheets, javascript files and also all links use relative paths, such as:
/css/styles.css
/js/jquery.js
/controller/action
And these go to the main domain like this:
www.mydomain.com/css/styles.css
www.mydomain.com/js/jquery.js
www.mydomain.com/controller/action
So how to make it work in that subfolder?
My .htacces file looks like this now:
RewriteEngine On
# Exclude some directories from URI rewriting
#RewriteRule ^(dir1|dir2|dir3) - [L]
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
EDIT: I have solved the problem by using setBaseUrl() method of front controller. Now the problem is how to edit my .htaccess file.
The best solution would be to set a BaseUrl in your application. This should be detected automatically if you set a RewriteBase in your .htaccess. The router and other components have a knowledge of this setting and can generate appropriate links.
There's a view helper which makes linking to assets easier.
Alternatively you can fix those links with a tag in the document head.
<html>
<head>
<base href="http://www.mydomain.com/temp/zend-application/">
</head>
Further reading at http://www.w3schools.com/TAGS/tag_base.asp
I would definetly use baseUrl() view helper preppended to the CSS's and JS's paths.

How do I use .htaccess to force www. while using Zend Framework

I want to force a www. prefix on my website by using a .htaccess 301 redirect. I am currently trying:
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
Which normally works, but I am using Zend Framework which causes all requests to be redirected back to http://www.mysite.com/index.php regardless of the initial request.
For example...
http://mysite.com/blog,
http://mysite.com/contact,
http://mysite.com/blog/this-is-my-article,
Will all be redirected to http://www.mysite.com/index.php
However, if I initially request a specific file, such as...
http://mysite.com/some-file.htm
The redirect works properly, redirecting to http://www.mysite.com/some-file.htm
In first time, don't forget to enable the rewriting ("RewriteEngine on").
The last line is important if you use Zend Framework.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteRule !\.(pdf|php|js|ico|txt|gif|jpg|png|css|rss|zip|tar\.gz)$ index.php
Now the url...
http://mysite.com/some-file.htm
... redirect to http://www.mysite.com/some-file.htm but use the index.php
Don't forget to ignore sub-domains when re-directing to www.
http://www.theblogaholic.com/2011/01/16/force-www-using-htaccess-except-for-subdomains/