【问题标题】:How to redirect the Index of / to a website?如何将/的索引重定向到网站?
【发布时间】:2021-06-30 08:07:27
【问题描述】:

我创建了一个 VirtualHost 来访问一个 php 文件,我想允许访问这个页面和 folder1 中的文件。 这是我的文件夹:

root
|mywebsite.php
|.htaccess
|otherfiles
|
|--images
|  |image.jgp
|
|--style
|  |style.css
|
|--folder1
|  |a_lot_of_folders

我可以拒绝访问根目录中的其他文件。如果 URL 是:www.ipaddress/www.ipaddress/otherfiles,它将重定向到 www.ipaddress/mywebsite.php。 但我想要的是只允许访问 php 文件和folder1 中的文件。但如果 URL 看起来像这样:www.ipaddress/folder1 或这样:www.ipaddress/style,它将打开 Index of /folder,我不想要这个。

这是我的 .htaccess :

RewriteEngine on

RewriteRule ^$ mywebsite.php
RewriteRule ^otherfiles$ mywebsite.php # This is for each files in the root folder except the php one
RewriteRule ^/folder1? mywebsite.php # This doesn't work

当 URL 有 /folder1 时,我尝试重写它,但它什么也没做

【问题讨论】:

    标签: .htaccess redirect url-rewriting


    【解决方案1】:

    试试这个作为你最后的规则:

    RewriteRule ^folder1/?$ mywebsite.php [L,NC]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^folder1/ mywebsite.php [L,NC]
    

    【讨论】:

    • REQUEST_FILENAME 是我应该改变还是像你一样写?
    • 它什么都没改变:/
    • 你在浏览器中输入了什么 URL 来测试这个,你得到了什么错误?
    • 您可能应该省略检查目录是否不存在的 -d 规则。您想将除现有文件之外的所有内容重写到 mywebsite.php
    • 那是因为您使用的是 css/images 的相对链接。您可以在页面 HTML 的 <head> 标记下方添加此代码:<base href="/" />
    猜你喜欢
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多