我有一个网站,我需要一个全能的非目录和非文件请求重定向回HTTP_HOST。我有一个重写规则,只提供无扩展的PHP文件,并始终从裸域请求解析到www.HTTP_HOST。这是我的htaccess文件:
#Main Page
DirectoryIndex index.php
AddDefaultCharset UTF-8
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Cache Control for Images and Assets
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# Never Use the naked domain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
# Never Use the naked domain and Remove PHP extension
RewriteCond %{THE_REQUEST} \.(php|cfm)[\s?] [NC]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)\.(cfm|php)$ /$1 [R=301,L,NE]
# Re-direct Non-file Non-directories to HTTP_HOST
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)((\?.*)|())$ http://www.example.com [R=301,L]
# unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
我的第四套规则似乎没有按照我需要的方式工作,不知道为什么。感谢您提前提供任何帮助。
答案 0 :(得分:0)
使用mod_rewrite
来考虑使用FallbackResource
,而不是使用{{1}}进行非文件请求过滤:
http://httpd.apache.org/docs/trunk/mod/mod_dir.html#fallbackresource
这更容易维护,并且Apache应该更容易处理。