htaccess获取没有路径的文件名 [英] htaccess get filename without path

查看:63
本文介绍了htaccess获取没有路径的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用htaccess获取RewriteCond的请求的不带路径的文件名. REQUEST_FILENAME返回完整的绝对路径,但是我只需要像 test.php

I'm trying to get the requested filename without the path with htaccess for a RewriteCond. REQUEST_FILENAME returns the full absolute path, but I only need the filename like test.php

我一直在寻找很多东西,但是找不到任何可以帮助我的东西. 感谢您的任何提前答复!

I've been searching for this a lot but couldn't find anything that helped me out. Thanks for any responses in advance!

我正在尝试执行以下操作:

Im trying to do something like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond _%{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]*)$ _$1.php [L]

  • 在浏览器中键入的URL如下所示: http://example.org/test
  • RewriteRule请求的文件为: http://example.org/_test.php
  • 我尝试使用RewriteCond _%{REQUEST_FILENAME}.php -f首先检查文件是否存在
    • The URL typed in the browser looks like this: http://example.org/test
    • The File that will be requestested by the RewriteRule is: http://example.org/_test.php
    • With RewriteCond _%{REQUEST_FILENAME}.php -f i tried to check if the file exists first
    • 基本上我想这样做:

      URI:/test/blah
      检查_ test.php 是否存在(带下划线!)

      URI: /test/blah
      Check if _test.php exists (with underscore!)

      推荐答案

      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{DOCUMENT_ROOT}/_$1.php -f
      RewriteRule ^([^/]+)$ _$1.php [L]
      

      我将*更改为+,因此要求example.com/不会重定向到_.php

      I changed * to + so requests for e.g. example.com/ will not redirect to _.php

      这篇关于htaccess获取没有路径的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆