htaccess 404 redirect problem - apache

htaccess 401 redirect problem
My problem is that my 404 redirect to my index.htm file is not working. The only way I can get it to work is by removing the addtype application line below, but I need this for an included left navigation on the site. Any help would be great thank you.
ErrorDocument 404 /
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$
RewriteRule .* products/noimage.jpg [L]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^cases.com [NC]
RewriteRule ^(.*)$ http://www.cases.com/$1 [L,R=301]
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# Rewrite Rule for blog.cases.com
RewriteCond %{HTTP_HOST} blog.cases.com$
RewriteCond %{REQUEST_URI} !blog/
RewriteRule ^(.*)$ blog/$1
AddType application/x-httpd-php .html .php .htm

I am not sure but some others had some conflicts using the AddType. Some variations that seemed to work for people are added below. Before trying and changing the AddType, put the complete file name in the 404 redirect.
ErrorDocument 404 /index.htm
EDIT:
I just tried your file, and I can't reproduce your issue. I looked at your hosting company's docs and you have it correct. The one thing I do see them mention is multiple extensions, and then takes you to a place showing the code (below) which you said didn't work
<FilesMatch "\.(php|htm|html)$">
SetHandler application/x-httpd-php
</FilesMatch>
They do show in the documentation the full domain listed and specifically mention "filename of the .html file" as shown below.
Custom Error Messages
Add the following to the .htaccess file:
ErrorDocument 404 http://forexample-domain.com/error.html
After "ErrorDocument", specify the error code, followed by a space, and then the path and filename of the .html file you would like to be displayed when the specified error is generated.
Source: http://support.verio.com/documents/view_article.cfm?doc_id=3624
If those don't work, then it probably is something with your hosting company. doesn't make to much since to me, and even if I tried to reproduce, couldn't. Sorry...

Related

Replace images that does not exist in htaccess

After series of search on stackoverflow, i got these two working htaccess codes, however, i'm confused which one is better.
Actually both works fine but is there any difference in performance or speed?
<FilesMatch "\.(jpg|jpeg|png|gif)$">
ErrorDocument 404 "/no_profile_picture.png"
</FilesMatch>
and
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(jpg|jpeg|gif|png|ico)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /no_profile_picture.png [L]
Both directives will serve the core purpose of showing /no_profile_picture.png when an image with jpg|jpeg|gif|png|ico extensions is not found inside your site root. However there is a subtle difference.
<FilesMatch "\.(jpg|jpeg|png|gif)$">
ErrorDocument 404 "/no_profile_picture.png"
</FilesMatch>
This directive will also return 404 (Http NotFound) status to browser/client but the other block will still return 200 (Http OK) status back to clients. So I suggest going with FilesMatch block for correctly showing desired non-found image and returning 404 to clients.

Strange behavior for /index.php/foo

I have a website at http://www.mywebsite.com/~me/ and I want these behaviors:
http://www.mywebsite.com/~me/index.php should redirect to http://www.mywebsite.com/~me/
A URL pointing to something that doesn't exist should redirect to http://www.mywebsite.com/~me/404.php
In looking around for solutions, I found these snippets to include in my httpd.conf file:
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteBase /~me/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~me/index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.mywebsite.com/~me/ [R=301,L]
and
ErrorDocument 404 /~me/404.php
These snippets create behavior 1 just fine, and for the most part behavior 2 as well. However, instead of http://www.mywebsite.com/~me/index.php/foo redirecting to the 404 page, it redirects to http://www.mywebsite.com/~me/index.php.
How would I change these snippets to get the behavior I want?

mod_rewrite in Users/<username>/Sites directory on OSX

There may be a fairly simple solution to this, but I've been searching for a couple of days and can't find one, soooo...
I'm developing a website on an OS X box (Lion). The working site is hosted at /Users/username/Sites and I've added that directory to /etc/apache2/users/username.conf. I can view the pages with no problems.
BUT... I'm using CodeIgniter and I want to remove the index.php from the URL. This should be a fairly simple job for mod_rewrite. I've added an .htaccess file to the directory (and set AllowOverride All in my conf file above). After googling around I discovered that I need Options +FollowSymLinks set (I did in the .htaccess file).
The problem with this is that it appears to rewrite the URL from localhost/~username/ to /Users/username/Sites. Problem with this is that, in that form, the browser simply attempts to DOWNLOAD the index.php file, rather than executing it. This gets worse when the links are /Users//Sites/index.php/controller/function because those files don't exist... CodeIgniter is meant to take over in the index.php, but only if it is executed.
So I can't remove the Options +FollowSymLinks because that generates Access Forbidden errors, and I can't leave it in for the reasons above.
Interestingly, putting exactly the same website to the /Library/WebServer/Documents directory works fine. OS X doesn't appear to mind FollowSymLinks to that directory, probably because it is set as the DocumentRoot in httpd.conf
My httpd.conf is stock Lion, except for AllowOverride All on /Library/Webserver/Documents. mod_rewrite is enabled.
My username.conf is
<Directory "/Users/username/Sites/">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
My .htaccess file in the site's directory is
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|static|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Clearly, I can develop on /Library/WebServer/Documents, but would prefer to do it in my local files.
There is an excellent wiki page on the CodeIgniter website about mod rewrite, it covers all of the changes you need to make to your .htaccess file and the CodeIgniter files itself.
It is easy to forget changing values in your config file like the index_page from:
$config['index_page'] = "index.php";
to
$config['index_page'] = "";
The example .htaccess file shown on that wiki page is (I have removed the comments):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
And if you find that you need to, just add the AddType and Options properties to it.
Here some of tricks may usefull
AddType application/x-httpd-php .php
Options +FollowSymLinks
RewriteEngine on
RewriteBase /Users/username/Sites/
RewriteEngine on
RewriteCond $1 !^(index\.php|static|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Also you can log rewriting log. But suggest you to remove these lines , after you fix everything
RewriteLog "/var/log/httpd/rewrite_log"
RewriteLogLevel 9

CakePHP 2.0.4 "URL rewriting is not properly configured on your server"

I've been trying to setup CakePHP on a development section of my server and I can't seem to solve the "URL rewriting is not properly configured on your server" error. I suspect I'm not configuring the .htaccess files with the correct RewriteBase. I've tried a wide variety of different RewriteBase for each file, but I can't seem to hit the right ones, and Cake doesn't give me any information other than "not working" (URL rewrite errors don't end up in Cake's error log).
I do not have access to my httpd.conf file, but I've used .htaccess and mod_rewrite with other frameworks (Wordpress and CodeIgniter) without a problem.
My base url for the site is: http://dev.domain.com/cake/
My base server path is: /home/username/public_html/dev/cake/
Cake root .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cake
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Cake app directory .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cake/app
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Cake webroot directory .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cake/app/webroot
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
Did you create a new default.ctp layout file and then "URL rewriting is not properly configured on your server." appeared?
If that is the case, it happened to me. It's working just fine. I think Cake is throwing a bad error message here.
Problem is with CSS.
If U don't make page on default layout of cake, and in your new layout You dont have default css: cake.generic.css it will show this error on page home.ctp. Look in code of home.ctp (View/Pages) and look - the id="url-rewriting-warning" of <p> where this error message is have in cake.generic.css value of display:none.
What does your vhost.conf look like? If you have a proper path in DocumentRoot, you shouldn't need to specify RewriteBase in .htaccess at all. Just stick with the defaults.
Try DocumentRoot /home/username/public_html/dev/cake/app in vhost, and remove RewriteBases from your .htaccess.
The problem for me was the css issue mentioned in another answer.
If you changed the css file in the default layout, you might need to add the following lines
to your new css file:
#url-rewriting-warning {
display:none;
}
... as long as rewriting appears to be working in all other ways.

Apache mod_rewriting not finding any files

OK, so we have a specific set of rewrites that we use on a number of sites due the file structure of our programming. It's never been a problem before until now.
For example, the rewrite:
RewriteRule ^$ Pages/news.php
Instead of directing to http://www.domain.com/wedding-venues-and-caterers-news/Pages/news.php it is redirecting to /path/to/file/public/wedding-venues-and-caterers-news/Pages/news.php and it's 404-ing (/path/to/file being the actual path).
If anyone could shed any light as to why this is happening, or point me in the right direction then I would be eternally grateful.
I am running all of my script and files in a subfolder, there is another .htaccess file in the root folder which I have edited in at the bottom but it doesn't seem to contain anything that could be interfering.
If you require any further information on the server then let me know!
EDIT - Here are all my rewrites as requested.
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^$ Pages/news.php
RewriteRule ^images/([0-9]+)/([0-9]+)/(0|1)/(.*).jpg?$ Classes/Image/timthumb.php?src=http://www.domain.com/wedding-venues-and-caterers-news/images/$4.jpg&h=$1&w=$2&zc=$3
RewriteRule ^([A-Za-z0-9\-]+)/([0-9]+)/([0-9]+)(/)?$ Pages/archives.php?cat_id=$2&page=$3 [NC,L]
RewriteRule ^archives/mon/([0-9]+)/yr/([0-9]+)/([0-9]+)$ Pages/archives.php?mon=$1&yr=$2&page=$3 [NC,L]
RewriteRule ^([A-Za-z0-9\-]+)/([0-9]+)(/)?$ Pages/archives.php?cat_id=$2 [NC,L]
RewriteRule ^archives/mon/([0-9]+)/yr/([0-9]+)(/)?$ Pages/archives.php?mon=$1&yr=$2 [NC,L]
RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)(/)?$ Pages/article.php?art_id=$3 [NC,L]
RewriteRule ^wedding-venues-catering-rss(/)?$ rss/rss.php [NC,L]
RewriteRule ^Page-Error(/)?$ Pages/errorPage.php [NC,L]
ErrorDocument 404 http://www.domain.com/wedding-venues-and-caterers-news/Page-Error
ErrorDocument 401 http://www.domain.com/wedding-venues-and-caterers-news/Page-Error
ErrorDocument 403 http://www.domain.com/wedding-venues-and-caterers-news/Page-Error
ErrorDocument 404 http://www.domain.com/wedding-venues-and-caterers-news/Page-Error
ErrorDocument 500 http://www.domain.com/wedding-venues-and-caterers-news/Page-Error
I also tried to reference the rewrites with absolute URL's, this worked of course, but they were just redirecting instead of rewriting as you might expect.
EDIT - This is the other .htaccess file in the root directory, which is part of their existing site.
#Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
<IfModule mod_rewrite.c>
RewriteEngine on
redirect 301 /Cavendish.htm http://www.domain.com/
redirect 301 /grange.htm http://www.domain.com/
redirect 301 /restaurant/index.html http://www.domain.com/
redirect 301 /news/Save-money-by-booking-wedding-venues-in-Middlesex-at-off-peak-times.asp http://www.domain.com/
redirect 301 /news/default.asp http://www.domain.com/
EDIT - I thought this might be relevant.
The 404 error on the rewrite shows this path -
/l/i/domain.com/public/wedding-venues-and-caterers-news/Pages/news.php
But.. if I edit news.php and request SCRIPT_FILENAME it gives me the path..
/services/webpages/l/i/domain.com/public/wedding-venues-and-caterers-news/Pages/news.php
My guess is that something is affecting the default path.
Furthermore I have tried basic rewrites at root level, but these also 404.
Finally the issue has been solved.
As the server is hosted with BT Business I decided to email them to see if they could help.
A guy replied saying he had logged in to the server and modified the .htaccess and it now works fine.
He simply added the subfolder in front of the rewritten URL's.
IE:-
RewriteRule ^$ Pages/news.php
Became: -
RewriteRule ^$ wedding-venues-and-caterers-news/Pages/news.php
I am still unsure why the subdirectory is needed when the .htaccess file is inside that directory, my guess is it is something to do with their server settings.