.htaccess删除扩展名,但允许404(如果不存在) [英] .htaccess remove extension but allow 404 if not existing

查看:95
本文介绍了.htaccess删除扩展名,但允许404(如果不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的Web服务器删除了.php上的文件扩展名。我只是想让它看起来更好。这很好。

I have removed file extensions on .php for my webserver. I just wanted to make it look nicer. This works excellent.

我有以下页面:
sitename.com/portfolio
sitename.com/portfolio/resume/file.html

I have the following pages: sitename.com/portfolio sitename.com/portfolio/resume/file.html

htaccess具有规则,因此当他们访问第二个链接时,它只是将其转换为Portfolio.php?resume& file = $ 1。

The htaccess has rules so that when they visit the second link its just converting it to portfolio.php?resume&file=$1. That works awesome.

好的,这是问题所在。因为我删除了扩展名,所以如果用户使用斜杠将域名键入sitename.com/ portfolio / ,他们将收到内部服务器错误。同样,如果他们只是键入sitename.com/portfolio/r,它们也将收到错误消息,因为它不存在。我知道一种解决方法是在htacess中为每个页面添加一个选项,但这似乎是一种不好的方法。

Okay heres the issue. Because i remove the extensions, if a user were to typ the domain as sitename.com/ portfolio/ with a traing slash, they will get an internal server error. Likewise, if they just type sitename.com/portfolio/r they also get an error since it doesnt exist. I know one way around this is to add an option in htacess for every page but that seems like a bad way of doing it.

无论如何,这里是我当前的htacess文件使用:

Anyways here the current htacess file I am using:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^portfolio/resumes/([^/]+).html$ /portfolio.php?resume&file=$1 [L,QSA]
RewriteRule ^portfolio/resumes$ /portfolio.php?resumes [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
ErrorDocument 404 /404.php

如果我访问的页面不存在,例如sitename.com/1或sitename.com/1.php,那么我得到404页面,但是如果1.php DID存在,那么我会看到它,如果确实存在,则输入sitename.com/1/或sitename.com/1/somethingelse,而不是404(我希望如此),那么它给了我一个内部服务器错误。有没有办法解决?我的语法错误?谢谢

If i visit the site on a page that doesnt exist like sitename.com/1 or sitename.com/1.php then I get my 404 page, but if 1.php DID exist then i would see it and IF it did exist, and I typed sitename.com/1/ or sitename.com/1/somethingelse then instead of a 404 (which I expect) then it gives me an internal server error. Is there any way around this? an error in my syntax? Thanks

推荐答案

这是因为当您请求带有尾部斜杠的uri时,例如: / uri / ,您的规则会将其重写为 /uri/.php 而不是 /uri.php ,这是一个未知/不受支持的目标。您需要接受斜杠作为可选字符。将规则更改为:

This is because when you request an uri with a trailing slash eg : /uri/ , your rule rewrites it to /uri/.php instead of /uri.phpwhich is an unknown/unsupported destination. You need to accept the trailing slash as an optional char. Change your rule to :

RewriteRule ^(.*?)/?$ /$1.php [L]

这篇关于.htaccess删除扩展名,但允许404(如果不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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