Apache的URL重写,

编程入门 行业动态 更新时间:2024-10-10 10:31:54
本文介绍了Apache的URL重写,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想获得URL重写工作,在我的网站。这里是我的.htaccess的内容:

I am trying to get URL rewriting to work on my website. Here is the contents of my .htaccess:

RewriteEngine On RewriteRule ^blog/?$ index.php?page=blog [L] RewriteRule ^about/?$ index.php?page=about [L] RewriteRule ^portfolio/?$ index.php?page=portfolio [L] #RewriteRule ^.*$ index.php?page=blog [L]

现在的3取消注释重写规则很好地工作,如果我尝试 www.mysite/blog / ,我重定向到 www.mysite/index的.php?网页=博客,同为关于和组合。不过,如果我输错博客,说我试试 www.mysite/bloh/ ,那么很明显,我得到一个404错误。最后一条规则的评论之一,是帮助prevent的。任何URL应该被重定向到博客,当然这个规则仍然是分析,即使我们已经成功地使用了previous之一,所以我用了最后一个标志([L])。如果我取消我的最后一个规则,任何东西,包括博客,关于和投资组合,重定向到博客。如果没有最后一个标志停止执行,只要它找到一个匹配的规则?

Now the 3 uncommented rewrite rules work perfectly, if I try www.mysite/blog/, I get redirected to www.mysite/index.php?page=blog, the same for "about" and "portfolio". However, if I mistype blog, say I try www.mysite/bloh/, then obviously I get a 404 error. The last rule, the commented one, was to help prevent that. Any URL should get redirected to the blog, but of course this rule is still parsed even if we have successfully used a previous one, so I used the "last" flag ([L]). If I uncomment my last rule, anything, including blog, about, and portfolio, redirect to blog. Shouldn't the "last" flag stop the execution as soon as it finds a matching rule?

感谢。

推荐答案

您可以添加一个条件,如:

You could add a condition like:

RewriteCond %{REQUEST_URI} !^index\.php

我还会提到,使用重写规则^ * $ 是打破所有的媒体请求(CSS,JS,图像),以及一个很好的方式。你可能想添加一些条件,比如:

I'll also mention that using RewriteRule ^.*$ is a good way to break all of your media requests (css, js, images) as well. You might want to add some conditions like:

RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f

要确保你没有试图改写的实际的服务器上存在的文件或目录。否则,他们将无法访问,除非index.php文件提供这些呢!

To make sure you're not trying to rewrite actual files or directories that exist on your server. Otherwise they'll be unreachable unless index.php serves those too!

更多推荐

Apache的URL重写,

本文发布于:2023-11-01 11:16:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549191.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   Apache   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!