我有一个隐藏文件夹hidden_folder
,服务器可以通过
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1
现在,我想将所有不可用的页面重定向到404错误页面内容。
插图1 :当用户尝试访问此处http://website.com/some_unavailable_uri
时,页面的网址不会更改,但内容将是/error/404.php中的内容
插图2 :http://www.apple.com/test。此页面不存在,网址不会更改,但内容显示错误。
基本上,它应该是if file does not exist, then redirect content
。我尝试过但没有成功。
首先尝试:
RewriteEngine On
RewriteCond hidden_folder%{REQUEST_URI} -f # %{REQUEST_URI} already has "/" at its beginning
RewriteRule ^.*$ error/404.php [NC,L]
第二次尝试:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} -f
RewriteRule ^.*$ error/404.php [NC,L]
有什么想法吗?
答案 0 :(得分:1)
条件也会在重定向上进行测试,因此您必须确保404重定向不会超出应有的范围。
这意味着%{REQUEST_URI}
一旦被重定向就会包含hidden_folder
,因此您必须添加RewriteCond
以避免它。
这应该有效:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/hidden_folder%{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !^/hidden_folder
RewriteRule ^(.*)$ error/404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1 [L]
答案 1 :(得分:1)
谢谢Outlyer。
实际工作代码是:
RewriteEngine On
# If the requested file is not in hidden_folder as a file
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} !-f
# AND
# If the requested file not is in hidden_folder as a folder
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} !-d
# AND
# If the requested file is not in root as a file
RewriteCond %{REQUEST_FILENAME} !-f
# AND
# If the requested file is not in root as a folder
RewriteCond %{REQUEST_FILENAME} !-d
# AND
# ???
RewriteCond %{REQUEST_URI} !^/hidden_folder
# THEN Redirect content
RewriteRule ^(.*)$ error/404.php [L]
####################################################
# Enable the deletion of hidden_folder in urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1 [L]
答案 2 :(得分:0)
您似乎只需要在目标中使用ErrorDocument
的简单http://
,因为这会导致在浏览器中更改网址。
使用.htaccess的这一行顶部:
ErrorDocument 404 /error/404.php
并且不要忘记评论你的重写规则。