Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess get filename without path

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!)

like image 989
Fabian Avatar asked Jan 26 '26 10:01

Fabian


1 Answers

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

like image 127
Gerben Avatar answered Jan 29 '26 10:01

Gerben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!