Apache:如何重写一些动态 URL

发布于 2024-12-08 11:55:26 字数 758 浏览 1 评论 0原文

我需要在我的网站上重写一些特定的 URL,但尽管搜索了相当长的时间仍无法找到如何做到这一点。

需要匹配的原始url格式如下: http://www.example.com/gallery/?level=picture&id =49

我需要它们采用以下格式: http://www.example.com/index.php ?page=gallery&level=picture&id=49

但是,该文件夹还包含一些图像文件。我需要将任何直接指向图像的 URL 保留下来,并且将任何指向 php 页面的 URL 都按照上面的方式重写。

我知道我想做什么,但不知道如何去做。基本上我需要在 .htaccess 中执行此操作:

if(url contains 'gallery/' AND filetype != bmp/jpg/png/etc){
REPLACE '/gallery/' WITH '/index.php?page=gallery&' AND append original query string variables
}

任何帮助都将不胜感激。

I need to rewrite some specific URL's on my website but cannot find out how to do it despite searching for quite some time.

The original url that needs to be matched is in this format:
http://www.example.com/gallery/?level=picture&id=49

and I need them to be in this format:
http://www.example.com/index.php?page=gallery&level=picture&id=49

However, the folder also contains some image files as well. I need any URL's pointing directly to images to be left alone, and any URL's pointing to a php page to be rewritten as above.

I know what I want to do, but not how to do it. Basically I need to do this in .htaccess:

if(url contains 'gallery/' AND filetype != bmp/jpg/png/etc){
REPLACE '/gallery/' WITH '/index.php?page=gallery&' AND append original query string variables
}

Any help at all would be greatly appreciated.

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

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

发布评论

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

评论(2

最后的乘客 2024-12-15 11:55:26

所以我找到了一个解决方案来解决这个问题。我现在可以在我的 .htaccess 文件中使用它。

RewriteCond %{REQUEST_URI} !\.(bmp|jpg|jpeg|png|gif)$
RewriteRule ^gallery/$ /index.php?page=gallery [R=302,QSA]

第一行(据我所知)排除了列出的文件类型受此规则的影响,因为正如我最初提到的,我不希望为图像重写 URL。

第二行采用这样的 url:
http://www.yoursite.com/gallery/?level=picture&id =52

并将其转换为:
http://www.yoursite.com/index.php ?page=gallery&level=picture&id=52

并且除了新的“page=gallery”变量之外,它还保留原始查询字符串。它还执行 302 重定向,以便用户在浏览器中显示正确的地址。

不确定这是否对任何人有帮助,但我想既然我发布了询问它,我也会发布我找到的解决方案。

So I have found a solution that has taken care of this problem for me. I have this working in my .htaccess file now.

RewriteCond %{REQUEST_URI} !\.(bmp|jpg|jpeg|png|gif)$
RewriteRule ^gallery/$ /index.php?page=gallery [R=302,QSA]

The first line (from what I have been told) excludes the file types listed from being affected by this rule, because as I originally mentioned I did not want the URL rewritten for images.

The second line takes a url like this:
http://www.yoursite.com/gallery/?level=picture&id=52

and turns it in to this:
http://www.yoursite.com/index.php?page=gallery&level=picture&id=52

and it leaves the original query string in place, in addition to the new "page=gallery" variable. It also does a 302 redirect so that the user is shown the correct address in their browser.

Not sure if this is helpful to anyone, but figured that since I posted asking about it, that I would post the solution I found as well.

意中人 2024-12-15 11:55:26

这可能比您想要的更笼统一些,但它也可以避免重写其他现有文件。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^/gallery/(.+)$    index.php [QSA]

This may a bit more general than you want, but it'll also avoid rewriting other existing files.

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