Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

.htaccess - Apache 404 url not working

I have a few problems with our company website. My colleague left the .htaccess file inside the _control directory like this:

# Virtual site directory
#
# This script lets us make use of default
# versions of files used in a typical site.
# When a request on the site is made for a file 
# that doesn't exist, instead of a 404 we rewrite
# the path to /_control/site/
#
# Any file in the directory this script is in will 
# take precedence over this script, so this script
# only comes into play if the file does not exist.

# First, set up URL rewriting
Options +FollowSymLinks
RewriteEngine On

# Add trailing slash for directories
# TODO: fix if site not in root dir (e.g. DEV server)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !.+.(php|js|css|gif|jpg|jpeg|png)$ [NC]
RewriteCond %{REQUEST_URI} !.*/$
RewriteRule ^(.*)$ /$1/ [L,R=301]

# This Apache mod_rewrite rule checks to see
# if the requested file exists, if it doesn't
# then we rewrite to the respective site location.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^.*/_control/(admin|common).*$
RewriteCond %{REQUEST_FILENAME} !^.*/_control/site/.*$
RewriteRule ^(.*)$ site/$1 [L]

The structure of my application looks like:

enter image description here

Everything was working OK but about a week ago, our mysite.com/admin and 404 page started redirecting to the hosting company's website. DaveG has already answered first part of the problem here: https://stackoverflow.com/a/26013322/1933380 and I was able to get the virtual directories working. I am still having problems with the 404 page. Am I missing something or making a mistake?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you are doing now, is telling the webserver to look for all non-existing pages and directories in another folder (_control). This does not give a solution for typo's, non-existing pages etc. If you want to solve this, you could use:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^.*/_control/(admin|common).*$
RewriteCond %{REQUEST_FILENAME} !^.*/_control/site/.*$
RewriteRule ^(.*)$ site/errhandler.php?file=$1 [L]

and make a file called errhandler.php, which displays a custom 404-page and (for example) give possible alternatives based on a database search or whatever. You could then use $_GET['file'] to display the original filename.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...