【问题标题】:Access only mydomain_name/filename.html仅访问 mydomain_name/filename.html
【发布时间】:2017-08-29 13:56:03
【问题描述】:

我需要拒绝所有排除的 URL 的访问:

我的域名/文件名.html

并将此请求重定向到:

我的域名/index.php?page=filename.html

并且用户应该在地址栏中看到:

我的域名/文件名.html

我希望所有样式和 js 脚本都能正常工作。

我该怎么做?

我正在尝试这个:

ErrorDocument 404 /
ErrorDocument 403 /

RewriteEngine On
RewriteBase /

RewriteRule ^(\w+)(?=\.).html$ index.php?page=$1.html

#if the request is for existent dirs, forbid the request
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^ - [R=403,L]

RewriteCond %{REQUEST_URI} !^/?$    
RewriteRule !^index.php$ - [R,F,L]

Options All -Indexes

我有服务器错误 500。

如果没有“Options All -Indexes”,js 文件将无法加载并且样式不起作用。

【问题讨论】:

    标签: php html .htaccess


    【解决方案1】:

    所以,我的最终 .htaccess 文件:

    RewriteEngine On
    
    ErrorDocument 403 http://mydomainname
    ErrorDocument 404 http://mydomainname
    
    # Don't show directory listings for URLs which map to a directory.
    Options -Indexes
    # Set the default handler.
    DirectoryIndex index.php
    
    # Internal redirect
    RewriteRule ^(\w+)(?=\.).html$ index.php?page=$1.html
    
    # Pass all requests not referring directly to files in the filesystem to
    # index.php. Clean URLs are handled in drupal_environment_initialize().
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !=/favicon.ico
    RewriteRule ^ / [L,R]
    

    此解决方案不是拒绝访问 mydomainname/include/myfile.php(如果此文件存在),但它涵盖了所有其他问题。

    【讨论】:

      【解决方案2】:

      在 .htaccess 文件中搜索重写条件。你会得到一个想法。或者添加 RewriteRule ^(.*).(gif|jpg|png|jpeg|css|js|swf)$ /public/$1.$2 [L,NC] 这个。

      【讨论】:

      • 这甚至没有开始回答OP的问题。
      猜你喜欢
      • 2019-04-16
      • 1970-01-01
      • 2010-11-26
      • 1970-01-01
      • 2012-12-20
      • 2015-06-08
      • 1970-01-01
      • 2021-12-13
      • 2016-06-11
      相关资源
      最近更新 更多