使用 htaccess 从 url 中删除 .php [英] Remove .php from urls with htaccess

查看:16
本文介绍了使用 htaccess 从 url 中删除 .php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前 .htaccess 文件:

current .htaccess file:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

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

我的网站托管在与大型托管帐户相关联的域的子文件夹中.

My site is hosted in a subfolder of a domain connected to a large hosting account.

basesite
  /iioengine
    /forums
      /.htaccess //file works
      /.... //other MyBB content
    /demos.php
    /index.php //iioengine.com (homepage)
    /.htaccess //file doesn't work
    /... //other iioengine php pages

问题是我使用了两个不同的 htaccess 文件吗?

Is the issue that I'm using two different htaccess files?

这是一个需要工作的链接:http://iioengine.com/demos

Here is a link that needs to work: http://iioengine.com/demos

我注意到这个当前的 htaccess 文件也破坏了所有论坛 URL

I noticed that this current htaccess file disrupts all of the forums URL's as well

这不再有效:http://iioengine.com/forums/Forum-Box2D

感谢您重新打开,我取得了一些进展.这是我当前的 htaccess 文件:

Thanks for reopening, I have made some progress. Here is my current htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>

我仍然得到 404 页,但如果我把这行输入:

I still get 404 pages, but if I put this line in:

重写规则./index.php [L]

所有非 '.php' 请求都被转发到主页...所以 mod_rewrite 肯定是启用的,它只是不能正常工作.有谁知道可能是什么问题?

all non-'.php' requests get forwarded to the homepage... So mod_rewrite is definitely enabled, it's just not working right. Anyone know what the issue could be?

编辑:这不重复 - 其他解决方案都不适合我.我的问题不是解决方案是否存在,而是为什么它们不适合我.没有人能够解决这个问题,我自己尝试了很多解决方案.这个论坛的意义不就是为了解决具体问题吗?

EDIT: This is not a duplicate - none of the other solutions work for me. My question is not do solutions exist, its why aren't they working for me. No one has been able to resolve this, I have been trying many solutions myself. Isn't the point of this forum to get solutions to specific issues?

请允许我澄清...

我在一个子文件夹中运行 MyBB,它的重写工作正常.例如,此链接有效:http://iioengine.com/forums/Forum-Box2D

I have MyBB running in a subfolder and its rewrites work fine. This link, for instance, works: http://iioengine.com/forums/Forum-Box2D

所有不属于 MyBB 的 php 页面在其 URL 中仍然具有 .php 扩展名 - 我正在尝试删除这些但没有任何效果.示例:http://iioengine.com/demos

All the php pages that are not part of MyBB still have the .php extension in their URLs - I am trying to remove these but nothing is working. Example: http://iioengine.com/demos

... [原帖]

显然有很多关于此的信息,但我尝试了近十几种不同的解决方案,但没有超过 404 页面.

There is obviously a lot of information out there about this, but I have tried almost a dozen different solutions and have not gotten past a 404 page.

这是我的网站:http://iioengine.com/,所有页面都是 php,其他所有页面比主页和所有论坛页面的 URL 末尾都有一个.php",我想删除它.

Here is my site: http://iioengine.com/, all pages are php, and everything other than the homepage and all of the forums pages have a '.php' at the end of their URL that I would like to remove.

除了将非.php"请求重定向到正确的页面之外,我还想删除.php"部分,即使它是请求的一部分(因为我的所有内容都已经指定了.php"' 在其超链接中).

In addition to redirecting non-'.php' requests to the correct pages, I would also like to remove the '.php' part even when it is part of the request (because all of my content already specifies '.php' in its hyperlinks).

这是我目前所拥有的,主要来自这篇文章,但它不起作用,我得到了一个 404 页面.

This is what I have so far, mostly taken from this post, but it doesn't work, I get a 404 page.

RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L,QSA]
RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule ^(.*)$ %1 [L,QSA]

在我的 htaccess 文件中需要什么才能在所有情况下从 URL 中删除文件扩展名?谢谢

what do I need in my htaccess file to remove the file extension from the URL in all cases? Thanks

推荐答案

试试这个隐藏 .php 的代码(两种方式都可以):

Try this code for hiding .php (will work both ways):

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

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

这篇关于使用 htaccess 从 url 中删除 .php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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