Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

regex - .htaccess how to remove file extensions and index files

I'm trying to achieve a few things through .htaccess ,but keep running into issues. Before you tell me I need to research better and there's already a solution on this or a different forum, please know I've already done that. I always try and figure out things on my own before coming here, but this one is truly stumping me. Everything I've tried has only partially worked. Any help or education here would be truly appreciated.

My site has the following simple structure:

(root)
 |   index.html
 |   .htaccess
 |
 |___portal-folder
      |     index.php
      |     home.php
      |
      |_____admin-folder
             |     index.php

I'm looking to achieve the following:

  1. When a user navigates to any base directory, for instance site.com or site.com/portal-folder/ they don't see the index file name index.html or index.php in their browser.

  2. Same holds true if the user navigates to the full URL site.com/index.html or site.com/portal-folder/index.php I would like the user to see site.com or site.com/portal-folder/ respectively in their browser.

  3. Strip the file extension off all files in the browser. So for instance navigating to site.com/portal-folder/home.php would show as site.com/portal-folder/home in the browser

The following code I'm using kind of works, but I'm getting strange behavior. For instance:

  • navigating to site.com/portal-folder/index doesn't remove the index file name and show up as site.com/portal-folder/index instead of site.com/portal-folder/ in the browser

  • navigating to site.com/portal-folder/ doesn't remove the index file name and shows up as site.com/portal-folder/index.php in the browser.

  • navigating to site.com/portal-folder/index.php takes the user back to the root site.com

  • navigating to site.com/portal-folder/home works correctly, but navigating to site.com/portal-folder/home.php doesn't strip the .php extension off.

  • navigating to site.com works correctly, but navigating to site.com/index.html doesn't remove the index file name.

    RewriteEngine On
    
    DirectoryIndex index.html  index.php
    
    # remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{THE_REQUEST} s(.+?)/+[?s]
    RewriteRule ^(.+?)/$ /$1 [R=301,L]
    
    # To internally forward /dir/file to /dir/file.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.+?)/?$ /$1.php [L]
    

Server Information: Apache Version 2.4.46


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have it this way:

DirectoryIndex index.html index.php

RewriteEngine On

# To externally redirect /dir/file.php to /dir/file and optionally remove index
RewriteCond %{THE_REQUEST} s/+(.*?/)?(?:index|(S+?))(?:.php|.html)?[/s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]

# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...