htaccess 中的重写规则以匹配某些文件扩展名

发布于 2024-09-29 05:49:59 字数 1160 浏览 1 评论 0原文

如何查找某些文件扩展名的实例,例如 .(jpg|png|css|js|php),如果没有匹配项,请将其发送到 index.php?route=$1。

我希望能够允许自定义用户名使用句点。

因此,将 http://example.com/my.name 重写为 index.php?route=my.name

Current setup:

。 htaccess:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ index.php?route=$1 [QSA,L]
</IfModule>

什么有效:
http://example.com/userpage -> index.php?route=用户页面
http://example.com/userpage/photos -> index.php?route=用户页面/照片
http://example.com/file.js -> http://example.com/file.js
http://example.com/css/file.css -> http://example.com/css/file.css

除了上述之外,我还需要做什么:
http://example.com/my.name -> index.php?route=my.name

How can I look for an instance for certain file extensions, like .(jpg|png|css|js|php), and if there is NOT a match send it to index.php?route=$1.

I would like to be able to allow period's for custom usernames.

So, rewrite http://example.com/my.name to index.php?route=my.name

Current setup:

.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ index.php?route=$1 [QSA,L]
</IfModule>

What works:
http://example.com/userpage -> index.php?route=userpage
http://example.com/userpage/photos -> index.php?route=userpage/photos
http://example.com/file.js -> http://example.com/file.js
http://example.com/css/file.css -> http://example.com/css/file.css

What I need to work in addition to above:
http://example.com/my.name -> index.php?route=my.name

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

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

发布评论

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

评论(2

只是一片海 2024-10-06 05:49:59

添加额外的 RewriteCond 以排除您不想重写的条件。在正则表达式之前使用 ! 来指示任何匹配的文件都不会满足条件。下面的 RewriteCond 未经测试,但应该可以让您了解您需要什么:

RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)$

Add an extra RewriteCond to exclude the conditions that you don't want rewritten. Use a ! before the regular expression to indicate that any files matching should fail the condition. The RewriteCond below is untested, but should give you an idea of what you need:

RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)$
度的依靠╰つ 2024-10-06 05:49:59

你有没有尝试过扭转逻辑?像这样的东西

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(jpg|png|css|js)$ - [L]

不会对任何具有 .jpg、.png、.css 或 .js 扩展名的文件进行重写。然后添加现有规则,以便将非文件、非目录请求重新路由到index.php。

Have you tried reversing the logic? Something like

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(jpg|png|css|js)$ - [L]

This will not do a rewrite for any file with a .jpg, .png, .css, or .js extension. Then add your existing rules so that non-file, non-directory requests get rerouted to index.php.

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