如何执行 mod_rewrite 重定向到相对 URL
我正在尝试实现漂亮 URL 的基本 URL 重定向,并且由于图像、CSS 等也驻留在同一路径中,我需要确保如果访问 URL 时没有尾随斜杠,则会自动添加它。
如果我像这样放置绝对 URL,则效果很好:
RewriteRule ^myParentDir/([A-Z0-9_-]+)$ http://www.mydomain.com/myParentDir/$1/ [R,nc,L]
但如果我将其更改为相对 URL,这样我就不必每次在文件夹中移动内容时都更改它,这根本不起作用。
这些是我尝试过的方法,但都不起作用,或者将我重定向到服务器的实际内部目录路径,例如 /public_html/... :
RewriteRule ^myParentDir/([A-Z0-9_-]+)$ ./myParentDir/$1/ [R,nc,L]
RewriteRule ^myParentDir/([A-Z0-9_-]+)$ myParentDir/$1/ [R,nc,L]
执行 URL 重定向的正确方法是什么,以便如果用户输入以下内容:
http://www.mydomain.com/somedir/myVirtualParentDir/myVirtualSubdir
他被重定向到(通过 HTTP 301 或 302):
http://www.mydomain.com/somedir/myVirtualParentDir/myVirtualSubdir/
谢谢。
编辑:添加更多细节,因为它似乎不清楚。 假设我正在实现一个画廊,并且我想使用 mod_rewrite 获得漂亮的 URL。 因此,我希望 URL 如下:
http://www.mydomain.com/somedir/galleries/cats
显示猫的缩略图,而:
http://www.mydomain.com/somedir/galleries/cats/persian
显示所有猫的缩略图中的一张图像,名为 persian。 所以实际上物理目录结构和重写如下:
http://www.domain.com/somedir/gallery.php?category=cats&image=persian
所以我想做的就是在 /somedir 中放置一个 .htaccess 文件,该文件捕获对 /galleries 发出的所有请求,并根据它后面的虚拟子目录,将它们用作重写中的占位符,有 2 个重写规则:
RewriteRule ^galleries/(A-Z0-9_-]+)/$ ./gallery.php?category=$1 [nc]
RewriteRule ^galleries/(A-Z0-9_-]+)/+([A-Z0-9_-]+)$ ./gallery.php?category=$1&image=$2 [nc]
现在的问题是图库脚本实际上需要一些 CSS、Javascript 和图像,位于 http://www.domain.com/somedir/css, http://www.domain.com/somedir/js 和 http://www分别为.domain.com/somedir/images。
我不想硬编码任何绝对 URL,因此将使用相对 URL(./css、./js、./images 等)引用 CSS、JS 和图像。所以我可以按如下方式重写 URL:
RewriteRule ^galleries/[A-Z0-9_-]+/css/(.*)$ ./css/$1 [nc]
问题是,因为 http://www.domain.com/somedir/galleries/ cats 是一个虚拟目录,以上仅在用户键入以下内容时才有效:
http://www.domain.com/somedir/gallaries/cats/
如果用户省略尾部斜杠 mod_dir 将不会添加它,因为实际上该目录实际上并不存在。
如果我使用绝对 URL 进行重定向重写,它就可以工作:
RewriteRule ^galleries/([A-Z0-9_-]+)$ http://www.mydomain.com/subdir/galleries/$1/ [R,nc,L]
但我不想对 URL 前缀进行硬编码,因为我希望能够将其放在我想要的任何子目录中的任何域上,所以我尝试了以下方法:
RewriteRule ^galleries/([A-Z0-9_-]+)$ galleries/$1/ [R,nc,L]
但它重定向到:
http://www.mydomain.com/home/myaccount/public_html/subdir/galleries/theRest
这显然不是我想要的。
编辑:进一步说明
我正在寻找的解决方案是避免对 .htaccess 中的域名或文件夹路径进行硬编码。我正在寻找一种解决方案,如果我将 .htaccess 与其余脚本和资源打包在一起,无论用户将其解压到他的 Web 服务器上,它都可以开箱即用。除了这个尾部斜线问题之外,所有的工作都是这样的。
因此,任何涉及以任何方式对 .htaccess 中的父目录或网络服务器路径进行硬编码的解决方案都不是我想要的。
I am trying to achieve a basic URL redirection for pretty-URLs, and due to images, CSS etc. also residing in the same path I need to make sure that if the URL is accessed without a trailing slash, it is added automatically.
This works fine if I put the absolute URL like this:
RewriteRule ^myParentDir/([A-Z0-9_-]+)$ http://www.mydomain.com/myParentDir/$1/ [R,nc,L]
But if I change this to a relative URL, so that I don't have to change it each time I move things in folders, this simply doesn't work.
These are what I tried and all do not work, or redirect me to the actual internal directory path of the server like /public_html/... :
RewriteRule ^myParentDir/([A-Z0-9_-]+)$ ./myParentDir/$1/ [R,nc,L]
RewriteRule ^myParentDir/([A-Z0-9_-]+)$ myParentDir/$1/ [R,nc,L]
What is the right way to do a URL redirection so that if the user enters something like:
http://www.mydomain.com/somedir/myVirtualParentDir/myVirtualSubdir
he gets redirected to (via HTTP 301 or 302):
http://www.mydomain.com/somedir/myVirtualParentDir/myVirtualSubdir/
Thanks.
EDIT: Adding some more details because it does not seem to be clear.
Lets say I am implementing a gallery, and I want to have pretty URLs using mod_rewrite.
So, I would like to have URLs as follows:
http://www.mydomain.com/somedir/galleries/cats
which shows thumbnails of cats, while:
http://www.mydomain.com/somedir/galleries/cats/persian
which shows one image from the thumbnails of all cats, named persian.
So in actual fact the physical directory structure and rewriting would be as follows:
http://www.domain.com/somedir/gallery.php?category=cats&image=persian
So what I want to do is put a .htaccess file in /somedir which catches all requests made to /galleries and depending on the virtual subdirectories following it, use them as placeholders in the rewriting, with 2 rewrite rules:
RewriteRule ^galleries/(A-Z0-9_-]+)/$ ./gallery.php?category=$1 [nc]
RewriteRule ^galleries/(A-Z0-9_-]+)/+([A-Z0-9_-]+)$ ./gallery.php?category=$1&image=$2 [nc]
Now the problem is that the gallery script in fact needs some CSS, Javascript and Images, located at http://www.domain.com/somedir/css, http://www.domain.com/somedir/js, and http://www.domain.com/somedir/images respectively.
I don't want to hardcode any absolute URLs, so the CSS, JS and Images will be referred to using relative URLs, (./css, ./js, ./images etc.). So I can do rewriting URLs as follows:
RewriteRule ^galleries/[A-Z0-9_-]+/css/(.*)$ ./css/$1 [nc]
The problem is that since http://www.domain.com/somedir/galleries/cats is a virtual directory, the above only works if the user types:
http://www.domain.com/somedir/gallaries/cats/
If the user omits the trailing slash mod_dir will not add it because in actual fact this directory does not actually exist.
If I put a redirect rewrite with the absolute URL it works:
RewriteRule ^galleries/([A-Z0-9_-]+)$ http://www.mydomain.com/subdir/galleries/$1/ [R,nc,L]
But I don't want to have the URL prefix hardcoded because I want to be able to put this on whatever domain I want in whatever subdir I want, so I tried this:
RewriteRule ^galleries/([A-Z0-9_-]+)$ galleries/$1/ [R,nc,L]
But instead it redirects to:
http://www.mydomain.com/home/myaccount/public_html/subdir/galleries/theRest
which obviously is not what I want.
EDIT: Further clarifications
The solution I am looking for is to avoid hardcoding the domain name or folder paths in .htaccess. I am looking for a solution where if I package the .htaccess with the rest of the scripts and resources, wherever the user unzips it on his web server it works out of the box. All works like that apart from this trailing slash issue.
So any solution which involves hardcoding the parent directory or the webserver's path in .htaccess in any way is not what I am looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是直接来自 Apache 文档 的解决方案(在“尾部斜杠”下)问题”):
这是解决方案,用于测试 REQUEST_URI 的尾部斜杠,然后添加它:
这是另一个解决方案,它允许您免除某些 REQUEST_URI 模式:
希望这些有帮助。 :)
Here's a solution straight from the Apache Documentation (under "Trailing Slash Problem"):
Here's a solution that tests the REQUEST_URI for a trailing slash, then adds it:
Here's another solution that allows you to exempt certain REQUEST_URI patterns:
Hope these help. :)
此规则应该向任何不是真实文件/目录的 URL 添加尾部斜杠(我相信这是您所需要的,因为 Apache 通常会自动对现有目录进行重定向)。
编辑:
为了防止 Apache 附加相对于文档根目录的路径,您必须使用 RewriteBase。因此,例如,在作为应用程序根目录的文件夹中,添加以下内容,这将覆盖物理路径:
This rule should add a trailing slash to any URL which is not a real file/directory (which is, I believe, what you need since Apache usually does the redirect automatically for existing directories).
Edit:
In order to prevent Apache from appending the path relative to the document root, you have to use RewriteBase. So, for instance, in the folder meant to be your application's root, you add the following, which overrides the physical path:
这可能有效:
但是,我不确定你为什么认为你需要这个。只需让你的 CSS / JS / 图像文件重写规则看起来像这样:
无论浏览器请求
/somedir/galleries/css/whatever.css
还是/ ,一切都应该正常工作somedir/galleries/cats/css/whatever.css
甚至/somedir/galleries/cats/persian/calico/css/whatever.css
。诗。此规则的一个问题是它会阻止您使用任何名称为“css”、“js”或“images”的画廊。您可能希望通过将这些虚拟目录命名为“.css”、“.js”和“.images”等名称,或者使用与有效图库名称不冲突的其他命名方案来解决此问题。
This might work:
However, I'm not sure why you think you need this at all. Just make your CSS / JS / image file rewrite rule look something like this:
and everything should work just fine regardless of whether the browser requests
/somedir/galleries/css/whatever.css
or/somedir/galleries/cats/css/whatever.css
or even/somedir/galleries/cats/persian/calico/css/whatever.css
.Ps. One problem with this rule is that it prevents you from having any galleries names "css", "js" or "images". You might want to fix that by naming those virtual directories something like ".css", ".js" and ".images", or using some other naming scheme that doesn't conflict with valid gallery names.
我不确定我是否完全理解你的问题。
由于 mod_dir 模块的存在,大多数 Apache 安装中会自动完成尾部斜杠重定向(99% 的可能性您拥有 mod_dir 模块)。
您可能需要添加:
但它是默认值。
所以。如果您访问
foo/bar
并且 bar 不是 foo 目录中的文件而是子目录,则 mod_dir 会执行重定向到foo/bar/
。我知道唯一可以打破这个问题的是 Option Multiviews ,它可能试图在目录。所以你可以尝试添加:
并删除所有 rewriteRules。如果你没有得到这个默认的 Apache 行为,你可能需要看看 mod-rewrite,但这就像使用核弹杀死蜘蛛一样。核弹的使用可能会非常棘手。
编辑:
对于 mod-rewrite 的尾部斜杠问题,您可以查看此文档说明,说明这应该有效:
I'm not sure I complelty understand your problem.
The trailing slash redirection is done automatically on most Apache installation because of mod_dir module (99% of chance you'have the mod_dir module).
You may need to add:
But it's the default value.
So. If you access
foo/bar
and bar is not a file in foo directory but a subdirectory then mod_dir performs the redirection tofoo/bar/
.The only thing I known that could break this is the
Option Multiviews
which is maybe trying to fin a bar.php, bar.php, bar.a-mime-extension-knwon-by-apache in the directory. So you could try to add:And remove all rewriteRules. If you do not get this default Apache behavior you'll maybe have to look at mod-rewrite, but it's like using a nuclear bomb to kill a spider. Nuclear bombs may get quite touchy to use well.
EDIT:
For the trailing slash problem with mod-rewrite you can check this documentation howto, stating this should work: