htaccess - 删除扩展名 - 允许或强制使用尾部斜杠而不破坏相关链接
并提前感谢您的帮助!
我想要实现的是:我想使用 htaccess 从文件名中删除文件扩展名 - 这就是我所做的。但是,当用户将浏览器指向没有扩展名的文件并添加尾部斜杠时,我会收到页面未找到错误。我发现了一些代码可以纠正这个问题,但是,当我实现这些代码时,它破坏了我与位于头部的 CSS 和 jQuery 的相对链接。
我确实希望保留所有文件的相对链接,但也希望允许单个文件的尾部斜杠。
这可能吗?
我的网站在这里:http://www.getagig.info
以及我当前的 htaccess 代码(仅删除扩展名如下)。
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
and thanks in advance for any help!
What I'm trying to achieve is this: I would like to remove the file extension from file names using htaccess - which is what I've done. However, when a user points their browser to a file without an extension and adds a trailing slash I get a page not found error. I found some code which corrected this, however, when I implemented the code, it broke my relative links to CSS and jQuery located in the head.
I really would rather like to keep relative links to all my files, but would also like to allow trailing slashes for individual files.
Is this possible?
My site is here: http://www.getagig.info
And my current htaccess code (which only removes extensions is below).
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许尝试:
编辑:我从您下面的评论中了解到您的问题是什么,但我查看了您的网站,我有点困惑为什么您要尝试这样做。
据我了解,您真正想要的是 yourwebsite.com/filename 或 yourwebsite.com/filename/ 使用 filename.php,在这种情况下,您最好使用如下所示的 .htaccess 规则:
这应该仍然允许要加载的 CSS 和 JS 文件,以及服务器上实际存在的任何文件或目录。如果您不希望人们实际调用“filename.php”,那么您可以为此设置单独的规则。
Perhaps try:
Edit: I understand from your comment below what your issue is, but I've taken a look at your site, and I'm a little confused why you're trying to do it this way.
From what I understand, all you really want is for yourwebsite.com/filename or yourwebsite.com/filename/ to use filename.php, in which case you're better off using an .htaccess rule like the following:
This should still allow your CSS and JS files to load, as well as any file or directory that physically exists on your server. If you do not want people to physically call "filename.php", then you can set-up a separate rule for that.
如果您将
/
附加到您的网址,我认为无法测试该文件是否存在于 RewriteCond 中。我在这里能想到的唯一解决方案是使用 htaccess 重定向到某个 php 文件,这将解决问题:
.htaccess:
.php file
I don't think it's possible to test if the file exists in RewriteCond if you have
/
appended to your url.Only solution I can think of here would be using htaccess to redirect to some php file which will sort things out:
.htaccess:
.php file
将行:添加
到我的页面
标记内非常适合我。它甚至可以编写 PHP 函数来自动执行此操作,这样您就不必在其他情况下费心进行复制/粘贴。
更新:
Adding the line:
to my page inside the
<head>
tag works great for me. It can be coded even a PHP function to automatize this so you don't have to bother with copy/paste on other occasions.Update: