使用 mod rewrite 重写 url,无需重定向 [英] rewrite url using mod rewrite without redirection

查看:33
本文介绍了使用 mod rewrite 重写 url,无需重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个网址正在被改写

http://domain.com/embed/slideshow-image-list.js

到这里

http://domain.com/code/embed/slideshow-image-list

当前的 htaccess 代码

current htaccess code

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
RewriteRule ^embed/([^\.]+)\.js$ http://domain.com/code/embed/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [L]

目前已正确重写,但如果您访问 .js 网址,它会重定向到完整网址.

its currently rewritten correctly, but if you visit the .js url it redirects to the full url.

我只是想在没有重定向的情况下映射它

im looking just to map it without redirection

推荐答案

不要在规则中提供域,只提供路径.使用完整域,它将被重定向.

Don't supply the domain in the rule, just the path. With the full domain it will be redirected.

RewriteRule ^embed/([^\.]+)\.js$/code/embed/$1 [L]

或者,如果生成的路径不是真正的文件并且应该由 index.php 处理,则去掉 [L] 以便执行下一条规则:

Alternately, if the resulting path is not a real file and should be handled by index.php, stip off the [L] so that the next rule will be executed:

RewriteRule ^embed/([^\.]+)\.js$/code/embed/$1

请注意,mod_rewrite 指南 说明了以下内容,这似乎是表示完整的 URL 路径是可以的,但我相信它与 ServerName 相比,不一定是 ServerAlias(我不是 100% 确定,但我在进行非重定向重写时总是使用路径而不是完整的 URL):

Note that the mod_rewrite guide says the following, which would seem to indicate that the full URL path is okay, but I believe it compares against ServerName, not necessarily ServerAlias (I'm not 100% sure about that, but I've always used paths rather than full URLs in doing non-redirect rewrites):

绝对网址

如果指定了绝对 URL,mod_rewrite 会检查主机名是否与当前主机匹配.如果是,则方案和主机名将被删除,结果路径被视为 URL 路径.否则,将对给定 URL 执行外部重定向.要强制外部重定向回当前主机,请参阅下面的 [R] 标志.

If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.

这篇关于使用 mod rewrite 重写 url,无需重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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