I am trying to improve the structure of my urls by using .htaccess. I have a file named foo.php
and a folder named /foo
. I want to be able to access example.com/foo
and show example.com/foo.php
. I also want to be able to access example.com/foo/bar
and show example.com/foo/bar.php
. Lastly I want to reditrect from example.com/foo/
to example.com/foo
. Does anyone know how to do this?
This is my code:
RewriteEngine On
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
I am able to remove the .php
extension by I am not able to redirect from the folder to the file. I also tried the solutions below but none of them resulted in the desired behaviour.
htaccess - Rewrite files that have a directory with the same name
.htaccess, rewriting of filename with same name as directory
.htaccess, proper rewriting of directory and file with same name
1条答案
按热度按时间ipakzgxi1#
I found a workaround solution to this problem. In order to remove the extension from file names, I used the following code in my .htaccess file:
The above code allows me to go to
example.com/foo
and showexample.com/foo.php
. In order to redirect fromexample.com/foo/
toexample.com/foo
, I created anindex.php
file in the/foo
directory. I added the following PHP code to the fileThis redirects from the directory to the file. Hope this helps.