我有这个httpd.conf …
<IfModule mod_rewrite.c> RewriteEngine On # Rewrite /foo/bar to /foo/bar.php RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L] </IfModule>
上述规则适用于eg://example.com/dir1/file,但对于://example.com/dir1/file?q =&category = 5&brand = BRAND,只需将其重写为://example.com/dir1 / file /?q =&category = 5&brand = BRAND,因为/ file / dir不会退出,所以抛出“页面未find”错误。
与[R]相同的规则正确地redirect到://example.com/dir1/file.php?q =&category = 5&brand = BRAND,但是我不想redirect。 我只是想“内部”重写。
可以请帮忙吗? 这是LAMP,Centos Apache 2。
(这个代码在PLESK,vhost.conf上工作正常)但是在httpd.conf上,它只是没有….
(我还没有尝试过你的第二个build议,但是我已经解决了这个问题)有一些免费的知识给Google员工: Apache版本很重要! 当你浏览文档时,请确保你看到了Apache版本的东西。
我的问题是,我用PLESK的一切。 vhost.conf中的这个块:
<IfModule mod_rewrite.c> RewriteEngine On # Rewrite /foo/bar/ to /foo/bar/index.php RewriteRule /([^.?]+)/$ %{REQUEST_URI}index.php [L] # FIX: If request_uri is just the domain name, then redirect to index.php. RewriteCond %{REQUEST_URI} ^/$ [NC] RewriteRule /(.*) %{REQUEST_URI}/index.php [L] # Rewrite /foo/bar to /foo/bar.php RewriteRule /([^.?]+)$ %{REQUEST_URI}.php [L] # Return 404 if original request is /foo/bar.php RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" RewriteRule .* /error [L,R] </IfModule>
像魅力一样工作。
但在CentOS / Apache 2上它却没有。 问题在于开始时的斜线和Apache的自动追踪斜线添加(来自较新版本的礼物)。 当一个脚本是名称/文件,并且存在一个名为/ file /的目录时,这个自动结尾的斜杠添加不是正确的。 所以我加了这两行:
<IfModule mod_rewrite.c> RewriteEngine On **DirectorySlash Off** # Rewrite /foo/bar/ to /foo/bar/index.php RewriteRule **^/?**([^.?]+)/$ %{REQUEST_URI}index.php [L] # FIX: If request_uri is just the domain name, then redirect to index.php. RewriteCond %{REQUEST_URI} ^/$ [NC] RewriteRule **^/?**(.*) %{REQUEST_URI}/index.php [L] # Rewrite /foo/bar to /foo/bar.php RewriteRule **^/?**([^.?]+)$ %{REQUEST_URI}.php [L] # Return 404 if original request is /foo/bar.php RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" RewriteRule .* /error [L,R] </IfModule>
多么酷啊?
这可能会让你更接近一点:
RewriteRule ^([^.?]+)(.*)$ $1.php$2 [L]
但其他人可能会提供更好的东西。
试试这个:
RewriteCond %{QUERY_STRING} (.*) RewriteRule ^(.*/?[^.]+)$ $1.php?%1 [L]
如果你所要做的只是从URL中隐藏文件扩展名,为什么不简单地为path启用“MultiViews”选项呢? 我经常在我的网站上这样做,所以我可以混合.html和.php页面,但离开了URL的扩展。
如果您为您的网页configuration了“AllowOverride All”或“AllowOverride Options”,您可以简单地添加一个.htaccess文件,内容如下:
Options +MultiViews
这将启用MultiView,Apache将使用MIME协商在path中查找可用文件,或者如果您完全控制了Apacheconfiguration文件,则可以简单地将“MultiView”添加到configuration文件中的“Options”行。
如果你想看到一个例子,你可以看看我的GPG政策URL在http://undergrid.net/legal/gpg这是完全从我的/legal/gpg.php页面运行。