访问页面.htaccess时出现不需要的404错误

时间:2015-03-12 00:47:17

标签: php apache .htaccess mod-rewrite

我有一个wesbite,其中有.htaccess文件,如下所示

ErrorDocument 404 /404
ErrorDocument 403 /404

Options -Indexes

# remove trailing slash
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)/$ /$1 [L,R=301]

#new Rule
#if its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#and it has a trailing slash then redirect to URL without slash
RewriteRule ^(.+)/$ /$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^domain\.in$ [NC]
RewriteRule ^(.*)$ http://www.domain.in/$1 [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index
RewriteRule ^index\.php$ / [L,R=301]

# remove .php from URL
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

# restrict .php
RewriteCond %{THE_REQUEST} \.php($|\ )
RewriteRule \.php$ / [F,L]

# remove .html
RewriteRule ^([^/.]+)\.html$ /$1 [L,R=301] 

RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
RewriteRule .* – [F,L]

AddType text/css .css 

<IfModule mod_expires.c>
ExpiresActive On
 ExpiresByType image/jpg "access 1 year"
 ExpiresByType image/jpeg "access 1 year"
 ExpiresByType image/gif "access 1 year"
 ExpiresByType image/png "access 1 year"
 ExpiresByType text/css "access 1 month"
 ExpiresByType application/pdf "access 1 month"
 ExpiresByType text/x-javascript "access 1 month"
 ExpiresByType application/x-shockwave-flash "access 1 month"
 ExpiresByType image/x-icon "access 1 year"
 ExpiresDefault "access 2 days"

</IfModule>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml x-font/otf x-font/ttf x-font/eot
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

<IfModule mod_headers.c>
  <filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=604800, public"
  </filesMatch>

  <filesMatch "\\.(css)$">
    Header set Cache-Control "max-age=604800, public"
  </filesMatch>

  <filesMatch "\\.(js)$">
    Header set Cache-Control "max-age=604800, public"
  </filesMatch>

  <filesMatch "\\.(xml|txt)$">
    Header set Cache-Control "max-age=604800, public, must-revalidate"
  </filesMatch>

  <filesMatch "\\.(html|htm|php)$">
    Header set Cache-Control "max-age=1, private, must-revalidate"
  </filesMatch>
</IfModule>

以上.htaccess禁止根据需要访问网站目录,但我面临一个问题:

  • 假设网站上有一个页面,如果我尝试通过http://domain.in/page_abc访问,那么也会显示404文档而不是实际页面。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我假设你使用的是Apache而不是IIS;

确保您已启用mod重写(您可以通过在httpd.conf文件中找到下面的行来执行此操作(通常在/ conf /目录中)如果您使用共享主机并从中删除分号,这可能会有所不同行的开头)

;LoadModule rewrite_module modules/mod_rewrite.so

Find the relevant directory tag for your www root and change AllowOverride None to AllowOverride All

在根目录中创建.htaccess文件(如果不存在)

相关问题