htaccess - 删除扩展名 - 允许或强制使用尾部斜杠而不破坏相关链接

发布于 2024-10-26 23:52:40 字数 466 浏览 2 评论 0原文

并提前感谢您的帮助!

我想要实现的是:我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

活雷疯 2024-11-02 23:52:40

也许尝试:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ $1.php

编辑:我从您下面的评论中了解到您的问题是什么,但我查看了您的网站,我有点困惑为什么您要尝试这样做。

据我了解,您真正想要的是 yourwebsite.com/filename 或 yourwebsite.com/filename/ 使用 filename.php,在这种情况下,您最好使用如下所示的 .htaccess 规则:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ $1.php

这应该仍然允许要加载的 CSS 和 JS 文件,以及服务器上实际存在的任何文件或目录。如果您不希望人们实际调用“filename.php”,那么您可以为此设置单独的规则。

Perhaps try:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ $1.php

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:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ $1.php

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.

栩栩如生 2024-11-02 23:52:40

如果您将 / 附加到您的网址,我认为无法测试该文件是否存在于 RewriteCond 中。

我在这里能想到的唯一解决方案是使用 htaccess 重定向到某个 php 文件,这将解决问题:

.htaccess:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule ^(.*?)/?$ route.php?url=$1

.php file

<?php

$url = $_GET['url'];

if (strpos($url, '..') !== false || strpos($url, '/') !== false) {
  echo "No relative urls!";

} else if (file_exists($url)) {
  // do what you want - require or redirect to the requested file

} else {
  // requested file does not exist
}

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:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule ^(.*?)/?$ route.php?url=$1

.php file

<?php

$url = $_GET['url'];

if (strpos($url, '..') !== false || strpos($url, '/') !== false) {
  echo "No relative urls!";

} else if (file_exists($url)) {
  // do what you want - require or redirect to the requested file

} else {
  // requested file does not exist
}
追风人 2024-11-02 23:52:40

将行:添加

<BASE href="http://www.sitename.com/">

到我的页面 标记内非常适合我。它甚至可以编写 PHP 函数来自动执行此操作,这样您就不必在其他情况下费心进行复制/粘贴。

更新:

<?php
$BaseDir = 'http://'.$_SERVER['SERVER_NAME'].substr($_SERVER["SCRIPT_NAME"], 0, strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
?>

<head>
...
<BASE href="<?php echo $BaseDir; ?>">
...
</head>

Adding the line:

<BASE href="http://www.sitename.com/">

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:

<?php
$BaseDir = 'http://'.$_SERVER['SERVER_NAME'].substr($_SERVER["SCRIPT_NAME"], 0, strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
?>

<head>
...
<BASE href="<?php echo $BaseDir; ?>">
...
</head>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文