Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess,重定向不存在,除了一些URL重写_.htaccess_Url Rewriting_Rewrite - Fatal编程技术网

.htaccess,重定向不存在,除了一些URL重写

.htaccess,重定向不存在,除了一些URL重写,.htaccess,url-rewriting,rewrite,.htaccess,Url Rewriting,Rewrite,我有一个网页,如果不存在的文件或文件夹是由用户键入的,或者只是转发到www.somewebsite.com。然后我想创建一个从www.somewebsite.com/about.html到www.somewebsite.com/about的网页,因为我认为越短越好。使用.htaccess我想我可以使用RewriteCond来表示不存在的内容,使用RewriteRule来表示用户友好的网页url。我在.htaccess方面很糟糕。我只知道基本知识,我做了研究,甚至是可能已经被问过的问题,但不确定如

我有一个网页,如果不存在的文件或文件夹是由用户键入的,或者只是转发到www.somewebsite.com。然后我想创建一个从www.somewebsite.com/about.html到www.somewebsite.com/about的网页,因为我认为越短越好。使用
.htaccess
我想我可以使用
RewriteCond
来表示不存在的内容,使用
RewriteRule
来表示用户友好的网页url。我在.htaccess方面很糟糕。我只知道基本知识,我做了研究,甚至是可能已经被问过的问题,但不确定如何编写此例外规则。

如何在
.htaccess
中添加代码,以便可以拥有除我指定的网页url之外的所有不存在的文件/文件夹?
。下面这一条将把所有不存在的文件重定向到index.html,当我访问www.somewebsite.com/about(from/about.html)时,将直接转到index.html。有什么帮助吗

--- my .htaccess shows --
# Redirect non-existing files or folders to index
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ / [L,QSA]
</IfModule>

# Rewrite existing files sample RewriteRule ^contact/$  /pages/contact.htm [L]
RewriteEngine on
RewriteRule ^/$    index.html [L]
RewriteRule ^about/$    about.html [L]
RewriteRule ^services/$    services.html [L]
RewriteRule ^application/$    application.html [L]
RewriteRule ^contact/$    contact.html [L]
RewriteRule ^privacy/$    privacy.html [L]
--my.htaccess显示--
#将不存在的文件或文件夹重定向到索引
重新启动发动机
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$/[L,QSA]
#重写现有文件示例重写规则^contact/$/pages/contact.htm[L]
重新启动发动机
重写规则^/$index.html[L]
重写规则^about/$about.html[L]
重写规则^services/$services.html[L]
重写规则^application/$application.html[L]
重写规则^contact/$contact.html[L]
重写规则^privacy/$privacy.html[L]

您需要在所有其他更具体的规则之后进行“全有或全无”重写(例如,
重写规则^(.*)$/[L,QSA]
。所有规则都按照它们出现的顺序应用,因此这意味着您的第一条规则总是被应用,然后重写引擎停止

交换订单,然后重试:

# Rewrite existing files sample RewriteRule ^contact/$  /pages/contact.htm [L]
RewriteEngine on
RewriteRule ^/?$    index.html [L]
RewriteRule ^about/$    about.html [L]
RewriteRule ^services/$    services.html [L]
RewriteRule ^application/$    application.html [L]
RewriteRule ^contact/$    contact.html [L]
RewriteRule ^privacy/$    privacy.html [L]

--- my .htaccess shows --
# Redirect non-existing files or folders to index
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ / [L,QSA]
</IfModule>

将永远不会被应用,因为在将规则应用到URI之前,会从URI中删除前导斜杠,因此
^/$
将永远不会匹配,您希望改为
^$
^/?$

我可能只会创建一个404错误页,并将重写改为不存在off@mythoslife那么,,您可以使用mod_dir的指令。很好,我使用了
ErrorDocument 404/404.html
我确信它是有效的。但是现在我所有的自定义URL页面都转到这个404.html页面。该死。@mythoslife ErrorDocument返回一个404错误,如果你想让它以静默方式返回一个没有404响应的页面,那么你需要使用FallbackResource。哦,你只需键入
FallbackResource/index.html
,而不是
RewriteCond
。听起来不错。我有点困惑。
RewriteRule ^/$    index.html [L]