在.htaccess多个mod_rewrite规则 [英] multiple mod_rewrite rules in .htaccess
问题描述
我有困难得到一些mod_rewrite规则在我的.htaccess文件一起工作。整个enitre网站,我想放弃了WWW。从所有URL。我使用下面的文档根:
I am having difficulty getting several mod_rewrite rules to work together in my .htaccess files. Throughout the enitre site I want to drop the "www." from all URLs. I am using the following at the document root:
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301]
那么,在一个文件夹/帮助我想要做2重写:
Then, in one folder "/help" I want to do 2 rewrites:
- 变化domain.com/help/1 到 domain.com/index.php?question=1
- 变化domain.com/help/category/example 到 domain.com/index.php?category=example
- change domain.com/help/1 to domain.com/index.php?question=1
- change domain.com/help/category/example to domain.com/index.php?category=example
因此,在domain.com/help我有以下几点:
So in domain.com/help I have the following:
Options +FollowSymLinks
RewriteRule ^([0-9]+)/?$ index.php?question=$1 [NC,L]
RewriteRule ^category/([^/\.]+)/?$ index.php?category=$1 [NC,L]
以上2 .htaccess文件工作:
www.domain.com的到 domain.com
domain.com/help/1的到 domain.com/index.php?question=1
domain.com/help/category/example的到 domain.com/index.php?category=example
The above 2 .htaccess files work for:
www.domain.com to domain.com
domain.com/help/1 to domain.com/index.php?question=1
domain.com/help/category/example to domain.com/index.php?category=example
但是,当我需要的2重写相结合,既丢弃这不起作用WWW。并重写子文件夹到一个URL变量。例如:
www.domain.com/help/1的到 domain.com/index.php?question=1
给出一个500错误。
But, this does not work when I need to combine the 2 rewrites to both drop the "www." and to rewrite the subfolders to a url variable. e.g.:
www.domain.com/help/1 to domain.com/index.php?question=1
gives a 500 error.
我上哪儿去了?而且,这是最好做2 .htaccess文件,或可/应在2个文件合并成文档根1 .htaccess文件?
Where did I go wrong? And, is this best to do with 2 .htaccess files, or can/should the 2 files be combined into 1 .htaccess file at the document root?
推荐答案
它看起来像正在发生的事情是在.htaccess文件中的 /帮助规则文件夹得到应用,因为你的要求一些文件夹中,因此父文件夹的规则将不会应用。你可以有你的父母的规则传下来的,如果你添加一个 RewriteOptions继承
在 /帮助文件夹的。htaccess的:
It looks like what's happening is the rules in the .htaccess file in the /help folder is getting applied because you're requesting something in that folder, so the parent folder's rules won't get applied. You can have your parent rules passed down if you add a RewriteOptions Inherit
in your /help folder's .htaccess:
Options +FollowSymLinks
RewriteOptions Inherit
RewriteRule ^([0-9]+)/?$ index.php?question=$1 [NC,L]
RewriteRule ^category/([^/\.]+)/?$ index.php?category=$1 [NC,L]
不过,继承的规则可能不会在你期望的顺序应用。例如,如果你要求 http://www.domain.com/help/1/ 你最终会得到重定向到 http://domain.com/index.php?question=1 这可能不是你想要什么,如果你想通过隐藏查询字符串,使搜索引擎友好的URL。
However, the inherited rules may not be applied in the order that you're expecting. For example, if you request http://www.domain.com/help/1/ you'll end up getting redirected to http://domain.com/index.php?question=1 which may not be what you want if you are trying to make SEO friendly URLs by hiding the query string.
您最好的选择可能是文件夹移动的东西在 /帮助到一个在您的文档根目录下,这样你就可以控制顺序的规则将被应用:
Your best bet may be to move the stuff in the /help folder into the one in your document root so that you can control the order that the rules will be applied:
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301]
RewriteRule ^help/([0-9]+)/?$ /index.php?question=$1 [NC,L]
RewriteRule ^help/category/([^/\.]+)/?$ /index.php?category=$1 [NC,L]
这保证了重定向到非www域名的出现第一个的,然后在 /帮助规则得到应用。所以,当你去 http://www.domain.com/help/1/ ,首先重定向到 http://domain.com/help/1/ 然后帮规得到应用和URI改写为 /index.php?question=1
。
This ensures the redirect to the non-www domain occurs first, then the /help rules get applied. So when you go to http://www.domain.com/help/1/, you first get redirected to http://domain.com/help/1/ then the help rules get applied and the URI is rewritten to /index.php?question=1
.
这篇关于在.htaccess多个mod_rewrite规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!