ZEND - How to make it works on a shared host? - php

I have to move a Zend website on a shared hosting which is using Pretty URL but css files doesn't load when params is added. Before moving, i want to test with server ip adress to be sure.
For exemple
-SERVER_IP/~username/en (works)
-SERVER_IP/~username/en/news (fails because it search in "en" folder)
I would like to redirect every css files from path /css/ to my real css folder which has many css files.
I want same thing for every path /images/ and /js/ and maybe others.
Can someone help me ?
thank you!
UPDATE 1
Does path relative change between domain.com and SERVER_IP/~username? When i set /css/style.css, it search for SERVER_IP/css/style.css instead of SERVER_IP/~username/css/style.css. If i can make it works for both, my problems will be gone. But I can't test with domain because i don't have any for the moment
UPDATE 2
I've modified my hosts file in C:\windows\system32\drivers\etc\ to simulate with a real domain. As i expect, relative path aren't working the same ways.
- SERVER_IP/~username/index.php <img src="/img/world.jpg" /> => SERVER_IP/img/world.jpg (BAD, note i've lose ~username)
- www.domain.com/index.php <img src="/img/world.jpg" /> => www.domain.com/img/world.jpg (perfect)
With that change, all my path are good now and no need to redirect anything. But another problem appears. Since i use www.domain.com, index.php is shown in the URL and I don't want this : www.domain.com/index.php/en
UPDATE 3
I've found this rule : RewriteRule ^index.php(.*)$ http://%{HTTP_HOST}/$1 [R=301,NC,L] . index.php is now remove but when i'm on www.domain.com/en, it looks like en is not in the request uri and all my links move to www.domain.com/en/en/...
I paste here my .htaccess content :
Options +FollowSymLinks
Options -Indexes
RewriteEngine on
RewriteBase /
DirectoryIndex index.php index.html
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^index.php(.*)$ http://%{HTTP_HOST}/$1 [R=301,NC,L]
UPDATE 4
I got it ! I describe my entire solution below!

COMPLETE FINAL SOLUTION
I've found a solution, everything is working #1 now on a shared hosting. Here is my complete solution, I hope it will save a lot of time to somebody.
1- Complete Zend Architecture (D=Directory, F=File)
- public_html/
(D) application/
(D) data/
(D) library/
(D) docs/
(D) nbproject/
(D) public/
(F) index.php
...
(F) .htaccess
2- Remove the basic .htaccess file in "public/" folder and place this one in the root folder "public_html/" (or ../public)
RewriteEngine On
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]
END !!! It's all you have to do, nothing more, no tricky or dirty code! This .htaccess file solve the DOCUMENT_ROOT config problem in httpd.conf file for shared hosting.
IMPORTANT NOTE
If your domain is not forwarded at that moment (OR you want to test before moving host from a server to another (my case)) and you want to access to your website with that kind of URL http://SERVER_IP/~username/, it will not works. The best solution is to temporaly edit your hosts file in C:\windows\system32\drivers\etc\ to add a line like this :
SERVER_IP domain.com It works great!

Related

Rewriting the URL's of 2 subfolders in a /public (root document) folder

Currently I have a "public" folder being root directory where all my frontend code goes.
But since my public folder contains both the website content and app content, I would like to split it more up.
So my idea was:
.htaccess (rewriting public url)
src (php backend code)
public/
web/
index.php
app/
account.php
So my problem is, I can easily go to the pages like this:
http://example.com/web/index.php
http://example.com/app/account.php
but I would like to remove "web" and "app" from the URL's...
http://example.com/index.php
http://example.com/account.php
How can I do that using .htaccess? I'm running apache 2.4.
EDIT
An example of what I've tried:
RewriteEngine On
RewriteCond %{REQUEST_URI} !app/
RewriteRule (.*) /app/$1
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1
Probably you need to tune up your requests. But a first approach is:
First, make sure that you have mod rewrite enabled.
Second, write the following in the .htaccess
RewriteEngine On
RewriteRule ^[/]?index.php$ /web/index.php [NS,NC,L]
RewriteRule ^[/]?account.php$ /app/account.php [NS,NC,L]
You will need to read the documentation to make other rules. You can read it here
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
Also pay special attention on the flags. Read the documentation to set the ones that applies to your situation.
I've tried so many different things now, but I finally figured it out.
RewriteEngine on
RewriteBase /
# Rewrite every web request
RewriteCond "%{DOCUMENT_ROOT}/web/%{REQUEST_URI}" -f
RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/web/$1"
# Rewrite every app request
RewriteCond "%{DOCUMENT_ROOT}/app/%{REQUEST_URI}" -f
RewriteRule "^(.+)" "%{DOCUMENT_ROOT}/app/$1"
# Remove .php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This works when having a setup:
public/ (root folder)<br>
web/ (contains website frontend)<br>
app/ (contains app frontend)<br>
.htaccess (contains the code above)

.htaccess if path (folder) doesn't exist request another path

By adding the language to my website i'm facing a problem.
Becouse i don't want to change all my urls from all my templates but i want to keep the language into my url i need to modyfy the .htaccess .
Basicly the problem is as folows: - To keep the language in url i have to change the site url:
ex:
without language: http://www.mysite.com/some/other/vars SITE_URL : http://www.mysite.com
with language: http://www.mysite.com/cn/some/other/vars changed SITE_URL : http://www.mysite.com/cn
when i request for css file SITE_URL/my_style/style.css , obiously the css will not be found in 2'nd case becouse is not in the folder cn/
How i can modyfy the .htaccess to change the requested path if the path is :
http://www.mysite.com/cn
http://www.mysite.com/es
http://www.mysite.com/fr
Originaly htaccess content is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Options All -Indexes
Options All -Indexes
Options All -Indexes
Thanks..it should be simple...but i can't do it since 2 hours.
EDIT:
All i want is:
If a request is made at: www.mysite.com/cn/any/thing/else/style.css (in this case css file will not exist), make the request at: www.mysite.com/any/thing/else/style.css
Was a long day with no progress ...haha. it happends.
I suggest to fix the issue by requesting correct url! That just would be right.
However this should work
RewriteRule ^(cn|es|fr)/my_style/(.+)$ /my_style/$2 [L]
Add this to the htaccess files that you have in your cn/es/fr folders, preferably before any rules that you may have there:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*)$ /$1 [L]
This should make it so a request for /my_style/style.css from within the cn folder will serve the one from the document root.

How to access an ZF2 application without “public” in URL

I completed my project on ZF2, then uploaded it on shared hosting. I would like a folder for the zend app with all the files inside, and then in the document root I would like the files from the public folder to be visible, because I want my URIs to look like www.example.com (not www.example.com/public/).
What I have done in those cases with ZF1 app is to throw an app directory in the document root and with your server's .htaccess equivalent made it not serve files from there (Deny from all). Then stick your gateway script into the document root and update the paths found in there including APPLICATION_ROOT and the path to the autoloader.
Hope this helps.
You can move the files inside /public to your webroot. However, please be aware that config files below your webroot are a security risk.
This one work.
RewriteEngine On
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]

Running Yii from subdir. Remove subdir with .htacces, while .css,.js and existing files and dirs still are available

I've been searching for 4 hours by now and I still can't get this to work.
I have the following directories in my webroot:
- application
- assets
- css
- config
- protected
- .htaccess
- ...
- .htaccess
- framework
- [Yii framework inside]
- .htaccess
The .htaccess in my webroot should redirect/'rewrite' all requests for whatever xxx/xxx to http://www.example.com/application/ as long as the requested file or directory does not exist.
This way requests for .css, .js and other files can still work.
In my config I made sure Yii expects SEO friendly URL's and I don't tend to use the index.php. So it now looks like this:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'' => 'site/index',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
'request' => array(
'baseUrl' => 'http://www.example.com',
),
The 'request' was added later on because Yii was generating links as example.com/application/site/login. With the request Yii generates example.com/site/login instead.
In the .htaccess in my webroot I tried about everything.
First I was able to 'remove' the subdir from the URL. The index page was shown.
I tried to add a rule so all none existing directories would be redirected to the same url.
My first rule broke, and 'application' was in the URL again, but no css styles were loaded.
At this moment I got the index page with css, but now everything brings me to the index page.
my .htaccess looks like this:
RewriteEngine on
RewriteCond $1 !^application/
# if a directory or a file exists, use it directly
RewriteCond %{DOCUMENT_ROOT}/application/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/application/$1 -d
RewriteRule ^(.*)$ application/$1 [L]
# otherwise forward it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^.*$ application/index.php
Mod_rewrite is enabled (I know because some things worked before). I looked at examples from the Yii docs.
I tried solutions from other questions on Stack Overflow like this one and many many others.
Still no luck.
Could someone please help me out?
edit:
With the .htaccess above a request to example.com ends at example.com/application .. I however would like to make the 'application' 'invisible' again (worked before, don't know why it broke)
I did change my .htaccess as follows:
RewriteEngine on
RewriteCond $1 !^application/
# if a directory or a file exists, use it directly
RewriteCond %{DOCUMENT_ROOT}/application/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/application/$1 -d
RewriteRule ^(.*)$ application/$1 [L]
# otherwise forward it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /application/
RewriteCond %{HTTP_HOST} ^(www.)?brainfeeder.be$
RewriteRule ^$ application/$1 [L,QSA]
But still a url like www.brainfeeder.be/site/login brings me to the default controller/action which is the site/index.
I guess my conditions or rules are not exactly correct yet.
Please see my small test application I set up to tackle this issue.
What happens: brainfeeder.be get rewritten to brainfeeder.be/application/ My Yii app is in there so it runs the 'bootstrap' index.php file and gets to the default controller/action, in this case site/index.
Now when you click the 'login' button it should show you a login form. But it stays at the site/index view.
Ok, once again I updated my .htaccess a couple of times. Now I have the following situation:
www.example.com AND example.com are rewritten to www.example.com/application AND example.com/application.
(www.)?example.com/existingfolder just shows content of 'existingfolder'.
(www.)?example.com/var1/var2/../varn get redirected to (www.)?example.com/application/var1/var2/.../varn
Now the only thing I would like to happen is that the latter gets rewritten instead of redirected. So visitors don't know they are in the directory 'application'.
So (www.)?example.com/var1/var2/.../varn would bring the visitor directly to the correct page.
The contents of my .htaccess at the moment:
Options +FollowSymLinks
#IndexIgnore */*
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?brainfeeder.be$
RewriteRule ^$ /application/ [L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
# The directory or file does not exist
RewriteCond %{REQUEST_FILENAME} !-s [OR]
RewriteCond %{REQUEST_FILENAME} !-l [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /application/$1 [L,R]
The thing is, when losing the R flag in the last RewriteRule will bring me to the index.php file inside 'application' but it shows the home page instead of a login form when for example I go to example.com/site/login.
Which, I guess, the script does not see the vars. (if it did site/error would trigger) So it handles this as a request to example.com/application and not as example.com/application/var1/var2
I hope I did explain the problem better this time.
Thanks for the great help 'till now.
Try to check these configuration directives if you just want to rewrite all the unexisting /$var1/$var2 to /application/:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /application/
Now, if you want those unexisting files to redirect, rather than to rewrite them? Then just add a [R] flag at the end of the RewriteRule directive, just don't forget a single " " space before the flag.
Now, if you want to redirect /application to / and then rewrite /index.php to /application and to rewrite also the unexisting /$var1/$var2 to /application/$var1/$var2 then it's quite hard (and need some exact details) but you could still try these configuration directives:
RewriteEngine on
# rewrite index.php
RewriteRule ^index.php$ /application
# rewrite unexisting files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /application/$1/$2
# try to remove this if it causes redirection loop
RewriteRule ^application/?$ / [R]
You can also try to use DirectoryIndex application/index.php at the very top of those directives to change the index of your site and remove the line RewriteRule ^index.php$ /application if it causes an error.
Actually, I can't understand your question.. You said:
The .htaccess in my webroot should redirect/'rewrite' all requests for
whatever xxx/xxx to http://www.example.com/application/ as long as the
requested file or directory does not exist. This way requests for
.css, .js and other files can still work.
And now, you said to your comment:
So any link to brainfeeder.be/application/$var1/$var2 should be shown
as brainfeeder.be/$var1/$var2
If you would also like to redirect existing /application/$var1/$var2 to /$var1/$var2 then please try to add these directives, and if it causes an error to your system, just remove it:
# the condition is important as mentioned above
RewriteCond %{REQUEST_URI} !\.css$
RewriteRule ^application/([^/]+)/([^/]+)/?$ /$1/$2 [R]
You can add another condition (as many as you like) at the top of the RewriteRule, just use your thinking if you're a programmer. You could add another condition like RewriteCond %{REQUEST_URI} !\.jpg$ if you doesn't want to redirect the file with an extension like .jpg or else that you want such:
RewriteCond %{REQUEST_URI} !\.css$
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteCond %{REQUEST_URI} !\.png$
RewriteRule ^application/([^/]+)/([^/]+)/?$ /$1/$2 [R]
Please try to change the source of your .htaccess file with this code:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?brainfeeder\.be$
RewriteRule ^/?$ /application/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /application/$1 [L]
Not sure what your question is ...
But here is an .htaccess that should accomplish what you want:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). After you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php under webroot (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After moving the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details I describe the approach more fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory

.htacces: page got called multiple times

I made a website that I run locally. I configured a my host file and virtual host:
Adress: website.dev
DocumentRoot: /www/website/public_html
I can access the website correctly.
In the public_html folder are the following files:
index.php
.htaccess
I tried to configure an .htaccess file, but I am not very experienced with that so maybe I did something wrong.
The idea is that every request like 'website.dev/users' or 'website.dev/login' is processed by the index file so I can handle the given URL. (I think that the URL: website.dev doesn't need a rewrite, because it directly leads to the index.php because it is the documentRoot)
This works fine but I noticed, when I use a rewriteRule in the .htacces file that my pages got opened called multiple times.
This is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
When I comment out all the rewriteRule lines, and go to: website.dev, I see that my pages only got called once. What in the .htaccess file can cause the double page calls?
Some of the conditions aren't required.
you're testing:
is the file size > 0 OR is a symbolic link, OR is directory OR is a regular file.
I've got the below which rewrites /users/id to: index.php?url=users/id
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !\.(jpg|css|js|gif|png)$ public/ [L]
RewriteRule !\.(jpg|css|js|gif|png)$ public/index.php?url=$1
Solution:
I found the problem by looking into the apache log files, so i noticed the webserver (wamp) looked for a .ico file (wamp logo) to show in the browsers addresbar, but in my project this file did not exists, that created a 404 and so the the index file was called again, i created the needed ico file in my project folder and everything worked like a charm.

Categories