[英]Remove .php extension with the help of .htaccess
I want to remove the .php
filename extension from the URL, I have already written code in the .htaccess
file but I am missing something because by default when I open the page it doesn't have the .php
extension, but if I manually add the .php
extension in the URL
then the page also opens, which I want to avoid. 我想从URL中删除
.php
文件扩展名,我已经在.htaccess
文件中编写了代码,但是我遗漏了一些东西,因为默认情况下,当我打开页面时,它没有.php
扩展名,但是如果我手动在URL
添加.php
扩展名,然后页面也会打开,我想避免这种情况。
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# rewrite category
RewriteEngine On
RewriteRule ^blog-category/(.*)$ blog-category.php?category=$1 [NC,L]
# rewrite blog
RewriteEngine On
RewriteRule ^blog/(.*)$ blog.php?title=$1 [NC,L]
# error pages
RewriteEngine On
ErrorDocument 404 /404.php
# on 301 error redirect to softcrayons.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^softcrayons.com
RewriteRule (.*) http://www.softcrayons.com/$1 [R=301,L]
You have nothing in that dynamic configuration file that actually prevents scripts being called directly. 该动态配置文件中没有任何内容可以阻止脚本直接被调用。 You have to add another redirection for that:
您必须为此添加另一个重定向:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?([^.]+)\.php$ $1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?([^.]+)/?$ $1.php [L]
This will force an external redirection (so change the URL visible in the browser) and a second request for all requests that use the .php
file name extension and where that file actually exists. 这将强制进行外部重定向(因此更改浏览器中可见的URL),并对所有使用
.php
文件扩展名以及该文件实际存在的位置的请求发出第二个请求。
Note that you may have to take care to not create an endless rewrite loop. 请注意,您可能必须注意不要创建无限的重写循环。
I also added some additional condition to only internally rewrite to .php
if that file actually exists. 我还添加了一些附加条件,以便仅在该文件实际存在的情况下内部重写为
.php
。
If you really want to create an error, a http status 404 for requests to URLs that have the .php
file name extension then replace the rewriting rule in the code above like that: 如果您确实要创建错误,请使用http状态404来请求扩展名为
.php
URL,然后替换上面代码中的重写规则,如下所示:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?([^.]+)\.php$ - [R=404]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?([^.]+)/?$ $1.php [L]
Note however that as already said I think that is a stupid thing to do. 但是请注意,正如我已经说过的,我认为这是一件愚蠢的事情。 Why frustrate your users with an error?
为什么用错误使用户感到沮丧? You know what they actually want and you can fulfill that request.
您知道他们的实际需求,并且可以满足该要求。 Think positive!
往正面想!
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files ( .htaccess
style files). 还有一个一般性提示:您应该始终喜欢将此类规则放置在http服务器(虚拟)主机配置中,而不是使用动态配置文件(
.htaccess
样式文件)。 Those files are notoriously error prone, hard to debug and they really slow down the server. 众所周知,这些文件容易出错,难以调试,并且确实降低了服务器的速度。 They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
仅当您无法控制主机配置(阅读:真正便宜的托管服务提供商)或者您的应用程序依赖于编写自己的重写规则(这显然是安全噩梦)时,才提供它们作为最后的选择。 )。
## 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]`
HTML: HTML:
<a href="home">Index</a>
.htaccess .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Link will redirect you to the home.php file, and your url will be example.com/home hope this will help you. 链接会将您重定向到home.php文件,您的网址将是example.com/home,希望对您有所帮助。
Greetings! 问候!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.