htaccess RewriteRule问题

发布于 2024-09-13 10:57:54 字数 448 浏览 3 评论 0原文

我的重写规则有问题。

我目前正在使用此规则:

RewriteRule ^([^/.]+)/?$ index.php?clip=$1

这对于以下链接效果很好,其中 $_GET['clip'] 是斜杠后的值:

http://example.com/abcdefg

但不适用于那些带有点字符的字符,例如:

http://example.com/abcdefg.png

我应该如何更改 RewriteRule 以使其适用于此类链接?

I have a problem with RewriteRules.

I'm currently using this rule:

RewriteRule ^([^/.]+)/?$ index.php?clip=$1

This works fine for links as these, where $_GET['clip'] is the value after the slash:

http://example.com/abcdefg

But not for those with a dot character, like these:

http://example.com/abcdefg.png

How should I change the RewriteRule to make it work for such links?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

晌融 2024-09-20 10:57:54
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?clip=$1

我不建议使用这种技术,因为它会导致额外的磁盘读取来检查文件是否存在......
如果您有可能,我建议您以 apache 可以从目录中知道要做什么的方式组织您的目录结构。我的框架中的示例:

  # skip whitelisted directories
  RewriteRule ^(test|skin|stylesheets|images|javascripts|favicon|robots\.txt|index\.php) - [L]
  RewriteRule ([-_\w\/]*) index.php?p=$1 [NE,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?clip=$1

i wouldn't suggest this technique because it will cause an extra disk read to check if the file is there...
if you have the possibility i would advise to organize your directory structure in a way that apache can tell from the directory what to do. example from my framework:

  # skip whitelisted directories
  RewriteRule ^(test|skin|stylesheets|images|javascripts|favicon|robots\.txt|index\.php) - [L]
  RewriteRule ([-_\w\/]*) index.php?p=$1 [NE,NC,QSA]
半步萧音过轻尘 2024-09-20 10:57:54

尝试这个代码:

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

这将防止它忽略真正在您的服务器上的文件。因此,只要 /abcdefg.png 存在,http://example.com/abcdefg.png 就可以工作。

此外,您在重写规则中使用的正则表达式需要稍微更改,我删除了 . 因为您正在阻止它。

try this code:

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

This will prevent it from ignoring files that are really on your server. so http://example.com/abcdefg.png will work, as long as /abcdefg.png exists.

Also the regex you used in the rewrite rule needs to be altered slightly, I removed the . as you were preventing it.

如果没结果 2024-09-20 10:57:54

您正在匹配任何不包含“/”或“.”的字符串。如果你希望匹配包含扩展名,你可以使用:

RewriteRule ^([^/]+)/?$

如果你不关心它:

RewriteRule ^([^/.]+)

You are matching on any string that does not contain "/" or ".". If you want the match to include the extension, you can use:

RewriteRule ^([^/]+)/?$

If you don't care about it:

RewriteRule ^([^/.]+)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文