简体   繁体   English

如何使用htaccess删除.php扩展名

[英]How to remove .php extension using htaccess

I have .htaccess file and inside it I have this code: 我有.htaccess文件,并且在其中有以下代码:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d

  RewriteRule ^ - [L]

  RewriteRule (.*) /cvsrrc/index.php [L]
</IfModule>

I tried to add this which removes the .php extension but, I got 404'd. 我试图添加它以删除.php扩展名,但是我得到了404。

RewriteRule ^(.*)$ $1.php

What I want to achieve is to add some code on my htaccess to remove the .php extension without removing the existing code above because I used that for my angular route. 我想要实现的是在htaccess上添加一些代码以删除.php扩展名,而无需删除上面的现有代码,因为我将其用于我的角度路由。 Thanks! 谢谢!

you can try this 你可以试试这个

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

edit: 编辑:

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

Try this. 尝试这个。 Got this from my project: 从我的项目中得到了这个:

#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

## 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]

As @starkeen says, just add the following line into my .htaccess code. 正如@starkeen所说,只需将以下行添加到我的.htaccess代码中。 And here it is : 这里是:

 Options +Multiviews

This one works for me 这个对我有用

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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