.htacces: page got called multiple times - php

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.

Related

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

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!

htaccess redirect only works if file doesn't exists

I want to redirect a file in my server to the same file in another server using htaccess and Redirect, my code is like:
Redirect /folder1/MyFile.zip http://www.otherserver/folder123/MyFile.zip
but it only seems to work if the file (or folder) in the original server doesn't exists, if I rename the folder it works fine, for sample.
I have to redirect the files one by one because they are on different paths in the new server (it's an online storage server) and don't want permanent redirects because in case of failure of the new server I only have to delete the htaccess file to get access again to the old files.
Thanks.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^assets/(.*)$ public/assets/$1 [L]
</IfModule>
Try this code.........

.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 deal with CSS, JS files when used with global apache redirect (for user friendly url's)

I have the following in my htaccess file:
RewriteEngine On
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
From rewrite.php I redirect to the correct pages depending on the url. Problem is that it redirects all files including css and js. I tried including these files but I now realise that was dumb of me. Should I redirect when there is and appropriate extension in the url? If redirecting is the way to go what method would be best? header location or HTTP_redirect?
Or is this not a good idea performance or work involved wise? I could go for something like this but I know next to nothing about apache and would rather not work with it right now.
RewriteRule ^(.*).css$ /includes/compressor.php?i=$1.css [L]
I previously had the following in my htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
I decided to remove this because:
I would not be able to include the header and other common files in the rewrite.php file. I would also not be able to have a database call in the rewrite file that would determine the page to include and to reuse the data for the page contents.
Unwanted files would be reachable such as service used only by external app.
The compression should be done once, and not for every request. You can then exclude requests from the URL rewriting if the corresponding file exists:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
How about redirecting only if the requested file does not exist on the server?
You could use the following rewrite conditions to achieve this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
So if the request is for a CSS/JS/HTML/Image file that already exists on the server then no rewriting takes place and the request for the file is processed. If the file does not exist, it will run your rewrite rule to rewrite.php

htaccess block access to directory but allow access to files

I have such situation.
I'm developing application in Zend Framework and htaccess pointing every request to
index.php. If some file or directory exists on the request path then htaccess allow access to this files like css, js, images etc...
Now I have for example such link:
example.com/profile/martin-slicker-i231
With Zend Router it points to controller account and action viewprofile. I want to add some avatart for my users and I created directory in public folder (so the images would be accessible by the server like css and js). The directory is named "profile" and subdirectory in it is "martin-slicker-i231". All path is visible by server like that:
public/
.htaccess
index.php
profile/
martin-slicker-i231/
avatar-small.jpg
The problem is when i point browser to example.com/profile/martin-slicker-i231 it
points me to this directory not the controller and action. When I remove the folder with user then the flow go to the controller and action. How to configure .htaccess so the example.com/profile/martin-slicker-i231 will be pointing to controller and action but request to example.com/profile/martin-slicker-i231/avatar-small.jpg point to the file. Here is my .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Can somebody help?
Just remove the portion that says that it should serve directories:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Now it will serve files (with size > 0) and symbolic links directly, but send directory references and other urls to your application
According to the Apache docs the following will turn off directory listings
<Directory /path/to/directory>
Options -Indexes
</Directory>
You might also want to look at the documentation for mod_autoindex

Categories