I'm trying to get all HTML and PHP files on my site to redirect through the index.php, so that they can have common frames applied to them, a templating solution I coded that I found to be quite elegant. Anywho, I'm having issues with my PHP files, specifically those that have arguments after them.
我试图让我的网站上的所有HTML和PHP文件重定向到index.php,这样他们就可以将常用的框架应用到它们,这是我编写的模板化解决方案,我发现它非常优雅。 Anywho,我的PHP文件有问题,特别是那些后面有参数的文件。
My regular rule for PHP files is the following:
我对PHP文件的常规规则如下:
RewriteCond %{REQUEST_URI} !index.php$
RewriteRule ^(.+).php$ index.php?page=$1&type=1 [NC,L]
This works fine for any pages that have no arguments, but you can see that any PHP documents that have
这适用于任何没有参数的页面,但您可以看到任何具有的PHP文档
?argument=something
end up as:
结束为:
index.php?page=path/to/page?argument=something&type=1
which is not a working solution at all. Now, what's bothering me here is the $ at the end of the rule, shouldn't that cause it to fail if there is anything after the .php?
这根本不是一个有效的解决方案。现在,困扰我的是规则结束时的$,如果在.php之后有什么东西,不应该导致它失败吗?
Anywho, I tried rewriting the rule as:
Anywho,我尝试重写规则为:
RewriteRule ^(.+).php\?(.+)$ index.php?page=$1&type=1&$2 [NC,L]
but that simply doesn't trigger at all. It seems that the regex flavor used in mod_rewrite is far different than I'm used to working with, so I'm sure these are simple mistakes I've made, but I can't seem to find decent documentation for this flavor of regex other than the most basic of examples.
但这根本不会触发。似乎mod_rewrite中使用的正则表达式风味与我以前使用的有很大不同,所以我确信这些都是我做过的简单错误,但我似乎找不到这种正则表达式的正确文档除了最基本的例子。
Can anyone show me what I'm doing wrong? Thanks.
谁能告诉我我做错了什么?谢谢。
1 个解决方案
#1
Try qsa in your rule, which stands for "query string append" - mod_rewrite will then append any query string from the original URL to the rewritten URL
在你的规则中尝试qsa,它代表“查询字符串追加” - 然后mod_rewrite会将原始URL中的任何查询字符串附加到重写的URL
RewriteCond %{REQUEST_URI} !index.php$
RewriteRule ^(.+).php$ index.php?page=$1&type=1 [NC,L,qsa]
RewriteRule doesn't match against the query string, which is why your second attempt did not work. Here's the relevant note from the manual
RewriteRule与查询字符串不匹配,这就是您的第二次尝试不起作用的原因。这是手册中的相关说明
The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.
模式将不与查询字符串匹配。相反,您必须使用带有%{QUERY_STRING}变量的RewriteCond。但是,您可以在替换字符串中创建包含查询字符串部分的URL。只需在替换字符串中使用问号,即表示应将以下文本重新注入查询字符串。如果要删除现有查询字符串,请仅使用问号结束替换字符串。要将新查询字符串与旧查询字符串组合,请使用[QSA]标志。
#1
Try qsa in your rule, which stands for "query string append" - mod_rewrite will then append any query string from the original URL to the rewritten URL
在你的规则中尝试qsa,它代表“查询字符串追加” - 然后mod_rewrite会将原始URL中的任何查询字符串附加到重写的URL
RewriteCond %{REQUEST_URI} !index.php$
RewriteRule ^(.+).php$ index.php?page=$1&type=1 [NC,L,qsa]
RewriteRule doesn't match against the query string, which is why your second attempt did not work. Here's the relevant note from the manual
RewriteRule与查询字符串不匹配,这就是您的第二次尝试不起作用的原因。这是手册中的相关说明
The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.
模式将不与查询字符串匹配。相反,您必须使用带有%{QUERY_STRING}变量的RewriteCond。但是,您可以在替换字符串中创建包含查询字符串部分的URL。只需在替换字符串中使用问号,即表示应将以下文本重新注入查询字符串。如果要删除现有查询字符串,请仅使用问号结束替换字符串。要将新查询字符串与旧查询字符串组合,请使用[QSA]标志。