htaccess 删除 .php 扩展名、index.php 并添加尾部斜杠 [英] htaccess remove .php extension, index.php AND add trailing slash

查看:22
本文介绍了htaccess 删除 .php 扩展名、index.php 并添加尾部斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是针对基本的 HTML/PHP 页面,没有查询字符串等等... 甚至添加一个斜杠.但是,每次我尝试全部使用它们或使用我找到的示例时,都会收到 500 服务器错误.

This is for basic HTML/PHP pages, no query strings, etc.. I have searched high and low and find resources for removing the 'index.php' from the URI, or removing '.php' and other file extensions.. and even adding a trailing slash. But, everytime I try to use them all, or use examples that I have found, I get a 500 server error.

我可以在一个页面上传递一个查询字符串,但实际上我只想从索引页面中删除index.php",并让所有子页面没有文件扩展名.所以,像这样:

I may pass a query string on one page, but really I just want to remove 'index.php' from the index page and have all sub pages without the file extension. So, something like this:

domain.com/index.php 到 domain.com/domain.com/page1.php 到 domain.com/page1/domain.com/page2.php 到 domain.com/page2/domain.com/page3.php 到 domain.com/page3/

domain.com/index.php to domain.com/ domain.com/page1.php to domain.com/page1/ domain.com/page2.php to domain.com/page2/ domain.com/page3.php to domain.com/page3/

我能找到的所有示例都集中在 CMS 等上.带有查询字符串等.没有什么只关注基本 URI,我认为这可能是导致我遇到错误的原因.

All the examples I can find are focused on CMSs, etc.. with query strings, etc.. nothing just focusing on the base URI, which I think might be what was causing the errors i was getting.

感谢任何帮助!谢谢

推荐答案

我曾经从一个论坛上的一个相当有帮助的人那里得到这个 - 从来没有完全理解它,还有一个警告;它意味着没有尾部斜杠,除非请求是一个目录.

I got this from a rather helpful chap on a forum once - never fully understood it, and there is one caveat; it implies no trailing slash unless the request is a directory.

但是,我认为值得发布 - 那里的专家可能很容易发现修复!?

However, I thought it was worth posting - a guru out there may easily spot the fix!?

# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{HTTP_HOST} ^www.domain.com
RewriteCond %{THE_REQUEST} ^GET (.*).php HTTP
RewriteRule (.*).php$ $1 [R=301]

# remove index
RewriteRule (.*)index$ $1 [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]

这篇关于htaccess 删除 .php 扩展名、index.php 并添加尾部斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆