在 .htaccess 中删除 .php 扩展名,同时保持已经工作的友好网址 RewriteRule。

huangapple go评论55阅读模式
英文:

Remove .php extension in .htaccess with already working friendly-url RewriteRule

问题

我尝试了这个网站上的数百个答案(其他用户的问题),但这些答案在我的情况下没有起作用。

我的网站使用slug来显示SEO友好的URL,这部分工作正常。但我还想要一些非动态链接/静态链接,例如:

example.com/contact.php 要显示/链接为没有 .php 扩展名的样式,例如 example.com/contact

但似乎什么都不起作用,经过12小时后,这个 .htaccess 让我发疯。

到目前为止,我已经做了以下工作:

RewriteEngine On

#这部分是用于SEO友好的URL,这部分有效
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([-/.a-zA-Z0-9]+)$ en.php?slug=$1 [QSA,L] 

#这部分是用于移除php扩展名的,这部分不起作用
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

有什么想法吗?

编辑
我像这样链接用户 example.com/contact,但我收到404错误。

英文:

I tried hundreds of answers from this site (questions from other users) but these answers did not worked in my case.

My website is using slugs to display seo-friendly urls and that part is working fine. But I would also like to have some NON dynamic links /static links like this:

example.com/contact.php to be displayed/linked without the .php extension, for example example.com/contact

But nothings seems to work, and after 12 hours this .htaccess is driving me crazy.

Here is what I have done until now

RewriteEngine On

#this is the part for see-friendly urls, this part is working
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([-/.a-zA-Z0-9]+)$ en.php?slug=$1 [QSA,L] 

#this is the part to remove php extension, this part is NOT working
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

Any idea?

EDIT
I am linking the users like this example.com/contact but I get error 404

答案1

得分: 1

重要的是要记住,重写规则不会“显示漂亮的URL”,它读取浏览器发送的URL,并决定如何处理它。思考“从URL中删除.php”将使你产生错误的思维方式。

你需要问的是,如果浏览器发送了一个URL是“/contact”,服务器应该决定显示哪个页面 - 是“/en.php?slug=contact”还是“/contact.php”?

还要记住文件中规则的顺序很重要 - 如果传入的URL与文件中的第一个规则匹配,服务器就不会继续查找文件以找到“更好”的匹配项。(在这个示例中尤其如此,因为你的第一个规则带有“L”标志,表示“最后”;如果没有这个标志,规则可能匹配的URL,但仍然不会替代原始规则。)

所以,在这种情况下的解决方案只是改变文件中规则的顺序:首先放置一个部分,检查是否存在名为“contact.php”的PHP文件,然后只有在该规则不匹配时,服务器才会进入部分,告诉它将“contact”用作slug。

英文:

The important thing to remember is that a rewrite rule doesn't "display a pretty URL", it reads the URL sent by the browser, and decides what to do with it. Thinking about "removing the .php from the URL" will put you in the wrong frame of mind.

What you need to be asking is, if the browser sends a URL of "/contact", how should the server decide what page to show - is it "/en.php?slug=contact", or is it "/contact.php"?

The other thing to remember is that order of rules in the file matters - if the URL coming in matches the first rule in the file, the server doesn't keep looking down the file for a "better" match. (This is particularly true in this example, since your first rule has the flag "L" for "last"; if that wasn't there, it would be possible for a rule to match the new URL, but it still wouldn't be instead of the original rule.)

So the solution in this case is simply to change the order of rules in the file: first put the section to check if there's a PHP file called "contact.php", and then only if that rule didn't match the server will reach the section that says to use "contact" as a slug.

huangapple
  • 本文由 发表于 2023年6月8日 05:16:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定