简体   繁体   中英

How to access a folder when the extension is removed via rewrite in the htaccess?

I have the following code in my .htaccess to remove the file extensions from my webpages. I like this because to me a website's URL looks cleaner.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

I'm having one issue with this though. I have a phpbb board sat in a folder in the root directory and i'd like to access the board in it's current position but i can't seem to access it properly.

So if i look at my root folder, it looks a little like this.

forum                   (folder)
.htaccess
page1.php
page2.php
page3.php

In the menu for my website, i link to the pages like this.

<a href="forum/">Forum</a>
<a href="page1">Page 1</a>
<a href="page2">Page 2</a>
<a href="page3">Page 3</a>

Trying to access the forum i get a 404 error, where the browser thinks that i'm looking to access www.myDomain.com/forum/.php. Obviously, this is wrong.

If i attempt to access the forum via www.myDomain.com/forum/ i also get a 404 error, where the browser also thinks that i'm trying to access www.myDomain.com/forum/.php. Obviously, this is wrong too.

I want both instances to go to www.myDomain.com/forum/index.php without explicitly stating index.php.

I've tried changing the .htaccess within the forum folder where a 404 error gets redirected to the index.php but this causes issues when logging in to the phpbb board for some reason.

If i don't have the Rewrite in it's current form in the .htaccess, then it all works perfectly but i then have to use the file extensions. The menu works using the links as shown and www.myDomain.com/forum/ finds the index.php.

What's the solution to this please? I'm sure there is one because stalkoverflow.com does a similar thing.

Thanks

This works like a charm. It checks to see if the directory has a .php extension before trying to add it to it.

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

I found this answer here Can't access index in folder when rewrite engine removes .php and .html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM