0

how can I show folder name instead of file name?

For example, www.example.com/home/index.php -> www.example.com/home

I know when I browse www.example.com/home I will get the result but that is not what I want because it will add a / behind the folder name (e.g www.example.com/home/).

What I want is without the / behind the folder and when user browse www.example.com/home/index.php the page will redirect the user to page not found. The purpose I do this is to hide the language I used and make the link more readable and memorable.

I found something like rewrite the rules in .htaccess file but I am new in php so I don't how to make it. Anyone can give me suggestion or provides some tutorial about this.

Thanks.

2 Answers 2

2

To remove the slash, you must first disable DirectorySlash

DirectorySlash Off DirectoryIndex disabled 

Direct access to PHP files can be answered with a R=404 status code

RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule \.php$ - [R=404,L] 

And then, you can rewrite requests pointing to a directory and containing an index.php

RewriteCond %{REQUEST_FILENAME}/index.php -f RewriteRule ^.*$ /$0/index.php [L] 

This RewriteCond looks, if the requested URL is a directory and if there is an index.php in this directory. If this is the case, then the index.php is executed.

Putting all together

DirectorySlash Off DirectoryIndex disabled RewriteEngine on # prevent direct access to PHP files RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule \.php$ - [R=404,L] # rewrite requests for a directory to index.php RewriteCond %{REQUEST_FILENAME}/index.php -f RewriteRule ^.*$ /$0/index.php [L] 
Sign up to request clarification or add additional context in comments.

5 Comments

Doesn't work. Whatever I browse will go to page not found. Example, I have www.example.com/home/index.php and I type www.example.com/home, it displays 404 error.
@overshadow I tried these rules in my test environment and they work fine. There must be additional rules in your htaccess or some other configuration issues causing this. Maybe, browser caching 301 redirects from your previous requests.
Can you explain this two line for me? RewriteCond %{REQUEST_FILENAME}/index.php -f RewriteRule ^.*$ /$0/index.php [L]
I found the solution. Just remove the / in front of the $0, from RewriteRule ^.*$ /$0/index.php [L] change to RewriteRule ^.*$ $0/index.php [L]. Thanks a lot.
@overshadow I tried to explain the RewriteCond and RewriteRule a bit, I hope it is clearer now.
0

Htaccess doesn't have anything common with PHP. Htaccess is Apache's configuration file, so you need to play with htaccess to achieve that what you want. On the other hand your solution will be really dirty. Learn about MVC and FrontController to make your structure cleaner.

1 Comment

Oh I see, thanks. Do you have any materials about MVC and FrontController for sharing with me?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.