.htaccess HTACCESS Directory vs rewrite cond [已关闭]

czfnxgou  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(59)

**已关闭。**此问题为not about programming or software development。目前不接受回答。

此问题似乎与a specific programming problem, a software algorithm, or software tools primarily used by programmers无关。如果您认为此问题与another Stack Exchange site的主题相关,可以发表评论,说明在何处可以回答此问题。
3天前关闭。
Improve this question
我有一个问题与我的网站.大多数的链接是设置到没有扩展名/page而不是page.html的页面名称.
htaccess文件中的这条规则使链接起作用。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

字符串
我们希望能够让用户使用domain.com/directory,而不必使用domain/directory/index.html

DirectoryIndex index.html index.php /index.html 
Options +Indexes


如果我打开了第1项的规则,那么第2项就不起作用。
我做错了什么?

****************HTACCESS FILE ********

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

DirectoryIndex index.html index.php /index.html
Options +Indexes


我已经尝试过删除上面提到的规则1或2,但是它修复了一个行为,却破坏了另一个行为。我怎样才能同时修复这两个行为呢?

vvppvyoh

vvppvyoh1#

好吧,问题是,你重写了一个目标目录的请求,该目录的内部文件名具有.html文件扩展名。这是因为你的条件RewriteCond %{REQUEST_FILENAME} !-f匹配,因为你不能有一个与目录同名的文件。这意味着下面的重写规则被应用:/directory => /directory.html。这不是你想要的。
我建议使用这种组合:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule ^ %{REQUEST_URI}.html [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule ^ %{REQUEST_URI}/.index.html [L]

字符串
所以你的完整配置文件看起来像这样(我冒昧地建议了一些修改):

RewriteEngine On

RewriteCond %{HTTPS} != on 
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule ^ %{REQUEST_URI}.html [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule ^ %{REQUEST_URI}/.index.html [L]


免责声明:
我还没有测试过这个,这只是一个建议,你需要 * 理解 * 这是什么,你不能简单地盲目地复制行,并期望一切正常工作.

相关问题