htaccess的重写/富/酒吧/到/foo/bar.php [英] .htaccess rewrite /foo/bar/ to /foo/bar.php

查看:152
本文介绍了htaccess的重写/富/酒吧/到/foo/bar.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待我的.htaccess文件中设置规则,以便以.php扩展名的文件都可以用只是一个斜杠(替换扩展/富/酒吧/ 将悄无声息加载 /foo/bar.php )。 previously,我发现这个,这是一种我正在寻找对,但它只是改变 /foo/bar.php /富/酒吧(注意没有结束斜线)。

I'm looking to set rules in my .htaccess file so that all files with a .php extension can be accessed by replacing the extension with just a slash (/foo/bar/ would silently load /foo/bar.php). Previously, I found this, which is kind of what I'm looking for, except it only changes /foo/bar.php to /foo/bar (notice there is no end slash).

我几乎完全的新手,当谈到htaccess的,约30分钟,从previous链接code调整由一吨 500内部服务器产生什么错误秒。我敢肯定,它是这样一个简单的修复,但无论谷歌还是反复试验还没有给我带来任何结果。

I'm almost a completely newbie when it comes to htaccess, and about 30 minutes of tweaking with the code from the previous link produced nothing by a ton of 500 internal server errors. I'm sure it's such a simple fix, but neither Google nor trial and error have yet to bring me any results.

任何人都可以看看从上述后(下图复制)的code和改变它,这样 /富/酒吧/ 来将改写 /foo/bar.php

Could anyone take a look at the code from the aforementioned post (copied below) and change it so that /foo/bar/ will rewrite to /foo/bar.php?

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

非常感谢!

推荐答案

每下面的意见,其目的是为了有一个像/富/酒吧/实际运行PHP脚本像/foo/bar.php用户可见的网址。另外,你可能希望用户谁试图加载/foo/bar.php直接重定向到/富/酒吧/的一致性。

Per the comments below, the intent is to have user-visible URLs like /foo/bar/ that actually run php scripts like /foo/bar.php. In addition, you probably want users who try to load /foo/bar.php directly to be redirected to /foo/bar/ for consistency.

#Redirect users loading /foo/bar.php to /foo/bar/
RewriteRule ^(.*)\.php$ $1/ [R]

#Rewrite /foo/bar/ (what the user sees) silently to /foo/bar.php
RewriteRule ^(.*)/$ $1.php [L]

[R] 在第一统治结束指定重定向,而不​​是改写,从而使用户负载/富/酒吧/。该 $ 1 在第二统治结束匹配括号(前终止斜线的一切),然后钉子.PHP就完了。该 [L] 表示,停止评估的规则集

The [R] at the end of the first rule specifies to redirect rather than rewrite, so that the user loads /foo/bar/. The $1 at the end of the second rule matches the parentheses (everything prior to the terminating slash) then tacks .php on the end. The [L] means to stop evaluating RewriteRules.

正如我在原来的答复中,用于替换字符串的领先斜线取决于上下文。有了这些规则,你就可以看到第一个规则的影响,因为它是一个重定向。如果重定向尝试访问 mydomain.comfoo /酒吧/ ,而不是 mydomain.com/foo/bar / ,然后在 $ 1 中的每个规则添加一个斜线。

As in my original reply, the use of a leading slash for the replacement string depends on context. With these rules you'll be able to see the effect of the first rule since it's a redirect. If the redirect tries to go to mydomain.comfoo/bar/ rather than mydomain.com/foo/bar/, then add a leading slash before the $1 in each rule.

另注 - 您的PHP脚本指望任何查询字符串?有可能需要一个附加的调整,如果它确实

Another note - does your php script expect any query strings? There may need to be an additional tweak if it does.

借助 URL重写参考有更多的细节。

这篇关于htaccess的重写/富/酒吧/到/foo/bar.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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