htaccess get filename without path

六月ゝ 毕业季﹏ 提交于 2020-01-02 19:21:07

问题


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!

Edit:

Im trying to do something like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond _%{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]*)$ _$1.php [L]
  • 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

Basically I want to do this:

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


回答1:


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

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



来源:https://stackoverflow.com/questions/10835848/htaccess-get-filename-without-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!