【问题标题】:.htaccess only redirects when target does not exist.htaccess 仅在目标不存在时重定向
【发布时间】:2015-01-20 15:08:36
【问题描述】:

我正在尝试使用.htaccessexample.com/short 之类的url 别名为example.com/short.php,但是当我测试配置时,只有在我请求不存在的php 脚本时才会被重定向。例如:页面short.php 存在,但shor.php 还没有当我请求example.com/short 的页面时,我得到example.com/short 的404 页面,但是当我请求example.com/shor 时,我得到example.com/shor.php 的404 页面

我正在使用的.htaccess 是:

RewriteEngine on
RewriteRule ^s/([A-Za-z0-9-]+)/?$ short.php?code=$1 [NC]
RewriteRule ^([A-Za-z0-9-]+)/?$ $1.php [NC,L]

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    在您的 .htaccess 中使用它:

    RewriteEngine on
    Options -MultiViews
    
    # skip all files and directories from rewrite rules below
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    RewriteRule ^s/([A-Za-z0-9-]+)/?$ short.php?code=$1 [NC,L]
    
    RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
    RewriteRule ^([A-Za-z0-9-]+)/?$ $1.php [NC,L]
    

    【讨论】:

      猜你喜欢
      • 2021-02-02
      • 2016-08-19
      • 2014-06-08
      • 2016-08-20
      • 1970-01-01
      • 2021-07-27
      • 2011-08-05
      • 1970-01-01
      相关资源
      最近更新 更多