.htaccess 内部重定向和 url 缩短的正确正则表达式 [英] Proper regex for .htaccess internal redirect and url shortening

查看:26
本文介绍了.htaccess 内部重定向和 url 缩短的正确正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于此线程:用于 .htaccess 重定向的正确正则表达式防止盗链

我在问一个更有针对性的问题(盗链已解决).

I am asking a more focused question (hotlinking is solved).

我正在做一个图像宿主项目并且正在编码图像名称.我需要为用户提供一个短网址来访问他们的图片.以下内容应在内部重定向:

I am doing an image host project and am encoding image names. I need to provide a short url for the users to access their images. The following should be internally redirected:

domain.com/hsh1h.jpg(或其他图片扩展名)
domain.com/hsh1h

domain.com/hsh1h.jpg (or other images extensions)
domain.com/hsh1h

这些将在内部重定向到:
domain.com/image.php?query=hsh1h.jpg
domain.com/image.php?query=hsh1h

These would be internally redirects to:
domain.com/image.php?query=hsh1h.jpg
domain.com/image.php?query=hsh1h

问题是不应重定向以下网址:

The issue is that the following url's should not be redirected:

domain.com/admin
domain.com/admin/index.php

domain.com/admin
domain.com/admin/index.php

我很抱歉之前创建了一个令人困惑的话题,非常感谢大家的帮助!

I apologize for creating a confusing topic earlier and thank you all so much for your help!

推荐答案

尝试:

# these rules ensure you aren't clobbering legit requests (like to image.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# just in case, exclude the admin directory
RewriteCond %{REQUEST_URI} !^/admin

# rewrite
RewriteRule ^(.*)$ image.php?query=$1 [L]

这篇关于.htaccess 内部重定向和 url 缩短的正确正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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