【问题标题】:Redirect for locations that don't exist but only in the root directory重定向不存在但仅在根目录中的位置
【发布时间】:2021-09-12 22:26:17
【问题描述】:

对于我的网站,我希望所有转到根目录(不是已经存在的文件)的请求都转到 redirect.php,但如果请求在子目录中,我不希望它重定向到该文件-directory 而是让那些转到通用错误页面。

这是我目前所拥有的:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /redirect.php?path=$1 [NC,L,QSA]

这是我的完整 .htaccess 文件

# Disabling directory listing
Options -Indexes -MultiViews +FollowSymLinks 

# Redirecting any traffic that doesn't exist to the index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /redirect.php?path=$1 [NC,L,QSA]

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    据我了解,您需要添加的只是一个条件,以防止在请求“子目录”时应用您的规则:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/.+/.+
    RewriteRule ^(.*)$ /redirect.php?path=$1 [L,QSA]
    

    或者你实现一个只匹配以“子目录”为目标的路径的规则:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^/?[^/]+$ /redirect.php?path=%{REQUEST_URI} [L,QSA]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      相关资源
      最近更新 更多