.htaccess 停止目录列表并转发到 handler.php
.htaccess stop directory listing and forward to handler.php
我正在尝试使用 .htaccess 和 PHP 实现 SEO 友好的 URL。
我已经实现了 90% 的目标,并且只在一个用例上苦苦挣扎。
这是什么快乐,当我访问以下url
http://somesite.com/cms/movies/tarzan
它降落在
my-handler.php?the_url=movies/tarzan
这是完美的,这就是我想要的,因为我自己管理它。真正的问题是当我不提供任何 slug 时,它会列出目录(意味着显示其中的所有文件和文件夹)
http://somesite.com/cms/
谁能帮我修复以下 .htaccess 内容,这样即使我不提供 slug,它仍应由 my-handler.php 处理,而不是列出完整目录?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cms/
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteRule ^my-handler\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /cms/my-handler.php?the_url= [L,QSA]
</IfModule>
Solved
根据 @Roman 的建议,我将以下内容添加到上面的列表中,它解决了我的问题。另外,我不必在访问物理目录时妥协 RewriteCond %{REQUEST_FILENAME} !-d
DirectoryIndex my-handler.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cms/
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteRule ^my-handler\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css|json|woff|ttf)
RewriteRule ^(.*)$ /cms/my-handler.php?the_url= [L,QSA]
</IfModule>
首先你必须设置 DirectoryIndex
来处理每个指向 /
的请求,你的 my-handler.php
。这可以通过将行添加到 .htaccess
来完成
DirectoryIndex my-handler.php
要禁用目录列表,您必须通过将以下行添加到 .htaccess
来禁止列表
Options -Indexes
请记住,此配置是针对 .htaccess
的,并且仅适用于您放置文件的目录。如果您想对整个网络服务器进行更改,您可以编辑 httpd.conf
并搜索对于
Options Indexes
并删除 Indexes
选项。
文档
列表由mod_autoindex module提供。
不错的小插曲
如果您只想禁用特定文件类型的列表,例如 .env
或 .php
文件,您可以向 IndexIgnore *.php
添加选项 .htaccess
我正在尝试使用 .htaccess 和 PHP 实现 SEO 友好的 URL。 我已经实现了 90% 的目标,并且只在一个用例上苦苦挣扎。
这是什么快乐,当我访问以下url
http://somesite.com/cms/movies/tarzan
它降落在
my-handler.php?the_url=movies/tarzan
这是完美的,这就是我想要的,因为我自己管理它。真正的问题是当我不提供任何 slug 时,它会列出目录(意味着显示其中的所有文件和文件夹)
http://somesite.com/cms/
谁能帮我修复以下 .htaccess 内容,这样即使我不提供 slug,它仍应由 my-handler.php 处理,而不是列出完整目录?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cms/
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteRule ^my-handler\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /cms/my-handler.php?the_url= [L,QSA]
</IfModule>
Solved
根据 @Roman 的建议,我将以下内容添加到上面的列表中,它解决了我的问题。另外,我不必在访问物理目录时妥协 RewriteCond %{REQUEST_FILENAME} !-d
DirectoryIndex my-handler.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cms/
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteRule ^my-handler\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css|json|woff|ttf)
RewriteRule ^(.*)$ /cms/my-handler.php?the_url= [L,QSA]
</IfModule>
首先你必须设置 DirectoryIndex
来处理每个指向 /
的请求,你的 my-handler.php
。这可以通过将行添加到 .htaccess
DirectoryIndex my-handler.php
要禁用目录列表,您必须通过将以下行添加到 .htaccess
Options -Indexes
请记住,此配置是针对 .htaccess
的,并且仅适用于您放置文件的目录。如果您想对整个网络服务器进行更改,您可以编辑 httpd.conf
并搜索对于
Options Indexes
并删除 Indexes
选项。
文档
列表由mod_autoindex module提供。
不错的小插曲
如果您只想禁用特定文件类型的列表,例如 .env
或 .php
文件,您可以向 IndexIgnore *.php
添加选项 .htaccess