从带有htaccess的url中删除.php

时间:2022-09-17 07:48:32

EDIT: current .htaccess file:

编辑:当前. htaccess文件:

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

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

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

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

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

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

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

This no longer works: http://iioengine.com/forums/Forum-Box2D

这不再适用:http://iioengine.com/forums/Forum-Box2D。

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

编辑:谢谢你重新打开,我已经取得了一些进展。这是我现在的htaccess文件:

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

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

我仍然有404页面,但如果我把这条线放进去

RewriteRule . /index.php [L]

RewriteRule。/索引。php[L]

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?

所有非”。php的请求被转发到主页……所以mod_rewrite肯定是被激活的,就是不太好用。有人知道这是什么问题吗?

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?

编辑:这不是重复-没有任何其他的解决方案适合我。我的问题不是解决方案是否存在,而是为什么他们不为我工作。没有人能够解决这个问题,我自己也一直在尝试很多解决方案。这个论坛的目的不就是要找到解决具体问题的办法吗?

Allow me to clarify...

请允许我澄清……

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

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

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

没有MyBB的所有php页面都有.php扩展名——我正在尝试删除这些,但是没有任何工作。例如:http://iioengine.com/demos

... [original post]

…(早前发布的文章)

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.

显然有很多关于这个的信息,但是我已经尝试了十几个不同的解决方案,还没有通过404页面。

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.

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

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).

除了重定向非'。php“请求到正确的页面,我也想删除”。即使是请求的一部分(因为我所有的内容都已经指定了)。php”超链接)。

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

这是我到目前为止的资料,大部分都是从这篇文章中摘录的,但是它不起作用,我得到了一个404页面。

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

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

在所有情况下,我需要在htaccess文件中删除URL中的文件扩展名吗?谢谢

5 个解决方案

#1


36  

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

尝试以下代码隐藏.php(两种方法都可以):

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]

#2


3  

try this.

试试这个。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L,QSA]
</IfModule>

if this not work then your sever do not have mod_rewrite activated or support url rewriting.

如果这不起作用,那么您的服务器没有激活mod_rewrite或支持url重写。

#3


0  

Here is the simplest syntax I have found, which would result as below:

下面是我发现的最简单的语法,其结果如下:

RewriteEngine on
RewriteRule ^/?PrivacyPolicy$ legal.php?mode=privacy [NC,L]
RewriteRule ^Report/([0-9]+)$ report.php?id=$1 [NC,L] 

Results:

结果:

  1. PrivacyPolicy to legal.php?mode=privacy
  2. PrivacyPolicy legal.php ?模式=隐私
  3. Report/55 to report.php?id=55
  4. 报告/ 55 report.php ? id = 55

#4


0  

This should work

这应该工作

Options -Indexes +FollowSymlinks -MultiViews

RewriteEngine on

DirectoryIndex index.php

# REDIRECT Force requests for named index files to drop the index file filename, and force non-www to avoid redirect loop
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.(html?|php[45]?)(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]*/)*)index\.(html?|php[45]?)$ http://example.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)/([^\.]+)\.php\ HTTP/
RewriteRule ^([a-zA-Z0-9_-]+)/([^.]+)\.php$ http://example.com/$1/$2 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)/([^/]+)/([^\.]+)\.php\ HTTP/
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([^.]+)\.php$ http://example.com/$1/$2/$3 [R=301,L]

# REDIRECT www to non-wwww
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule .? http://example.com%{REQUEST_URI} [R=301,L]

# REWRITE url to filepath
RewriteRule ^([a-zA-Z0-9_-]+)/([^/.]+)$ /$1/$2.php [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([^/.]+)$ /$1/$2/$3.php [L]

#5


0  

On apache 2.4 and later, You can use the following Rule with END flag to remove .php extension from urls.

在apache 2.4和以后的版本中,您可以使用以下规则和END标志从url中删除.php扩展。

RewriteEngine on

RewriteRule ^(.+)\.php$ /$1 [NC,L,R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ /$1.php [END]

#1


36  

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

尝试以下代码隐藏.php(两种方法都可以):

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]

#2


3  

try this.

试试这个。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L,QSA]
</IfModule>

if this not work then your sever do not have mod_rewrite activated or support url rewriting.

如果这不起作用,那么您的服务器没有激活mod_rewrite或支持url重写。

#3


0  

Here is the simplest syntax I have found, which would result as below:

下面是我发现的最简单的语法,其结果如下:

RewriteEngine on
RewriteRule ^/?PrivacyPolicy$ legal.php?mode=privacy [NC,L]
RewriteRule ^Report/([0-9]+)$ report.php?id=$1 [NC,L] 

Results:

结果:

  1. PrivacyPolicy to legal.php?mode=privacy
  2. PrivacyPolicy legal.php ?模式=隐私
  3. Report/55 to report.php?id=55
  4. 报告/ 55 report.php ? id = 55

#4


0  

This should work

这应该工作

Options -Indexes +FollowSymlinks -MultiViews

RewriteEngine on

DirectoryIndex index.php

# REDIRECT Force requests for named index files to drop the index file filename, and force non-www to avoid redirect loop
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.(html?|php[45]?)(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]*/)*)index\.(html?|php[45]?)$ http://example.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)/([^\.]+)\.php\ HTTP/
RewriteRule ^([a-zA-Z0-9_-]+)/([^.]+)\.php$ http://example.com/$1/$2 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)/([^/]+)/([^\.]+)\.php\ HTTP/
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([^.]+)\.php$ http://example.com/$1/$2/$3 [R=301,L]

# REDIRECT www to non-wwww
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule .? http://example.com%{REQUEST_URI} [R=301,L]

# REWRITE url to filepath
RewriteRule ^([a-zA-Z0-9_-]+)/([^/.]+)$ /$1/$2.php [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([^/.]+)$ /$1/$2/$3.php [L]

#5


0  

On apache 2.4 and later, You can use the following Rule with END flag to remove .php extension from urls.

在apache 2.4和以后的版本中,您可以使用以下规则和END标志从url中删除.php扩展。

RewriteEngine on

RewriteRule ^(.+)\.php$ /$1 [NC,L,R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ /$1.php [END]