Mod rewrite 重写规则有问题吗

Mod rewrite 重写规则有问题吗,mod-rewrite,redirect,Mod Rewrite,Redirect,我有一个应用程序,大部分仍在开发中,这就是为什么我需要阻止所有页面的访问,但不是其中两个页面 假设我有a.php,除了b.php之外,所有请求都将重定向到这里 所以我认为应该有3条规则: 1st: when a.php and b.php are requested they should be visible to user, 2nd: if anything other than those two is requested, it should be redirected t

我有一个应用程序,大部分仍在开发中,这就是为什么我需要阻止所有页面的访问,但不是其中两个页面

假设我有a.php,除了b.php之外,所有请求都将重定向到这里

所以我认为应该有3条规则:

1st: when a.php and b.php are requested they should be visible to user,

2nd: if anything other than those two is requested, 
     it should be redirected to a.php.

3rd: for external css,javascript and image files 
     there should be an access rule
由于我在服务器管理方面没有太多经验,我相信我完全迷路了:)

这就是我所尝试的:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !^/b.php
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ a.php

我不知道你说的#3是什么意思,但从你所尝试的来看,我猜你的意思是,所有的js、ico,。。。将被重定向到a.php。是这样吗

如果是,这意味着a=>a.php(带参数)、b=>b.php(带参数)和other=>a.php。所以试试这个:

RewriteBase /
RewriteRule ^a\.php(.*) /a.php$1
RewriteRule ^b\.php(.*) /b.php$1
RewriteRule ^.*         /a.php
RewriteBase /
RewriteRule ^a\.php(.*)                          /a.php$1
RewriteRule ^b\.php(.*)                          /b.php$1
RewriteRule ^(.*)\.(js|ico|txt|gif|jpg|png|css)$ /$1.$2
但如果您的意思是所有这些脚本和媒体文件都可以正常访问,请尝试以下操作:

RewriteBase /
RewriteRule ^a\.php(.*) /a.php$1
RewriteRule ^b\.php(.*) /b.php$1
RewriteRule ^.*         /a.php
RewriteBase /
RewriteRule ^a\.php(.*)                          /a.php$1
RewriteRule ^b\.php(.*)                          /b.php$1
RewriteRule ^(.*)\.(js|ico|txt|gif|jpg|png|css)$ /$1.$2
基本上,重写规则是正则表达式和$1,$2,。。。是匹配字符串组(用“(“and”)包装的字符串组)


希望能有所帮助。

我不确定你对#3的意思,但从你的尝试来看,我猜你的意思是,所有的js、ico,。。。将被重定向到a.php。是这样吗

如果是,这意味着a=>a.php(带参数)、b=>b.php(带参数)和other=>a.php。所以试试这个:

RewriteBase /
RewriteRule ^a\.php(.*) /a.php$1
RewriteRule ^b\.php(.*) /b.php$1
RewriteRule ^.*         /a.php
RewriteBase /
RewriteRule ^a\.php(.*)                          /a.php$1
RewriteRule ^b\.php(.*)                          /b.php$1
RewriteRule ^(.*)\.(js|ico|txt|gif|jpg|png|css)$ /$1.$2
但如果您的意思是所有这些脚本和媒体文件都可以正常访问,请尝试以下操作:

RewriteBase /
RewriteRule ^a\.php(.*) /a.php$1
RewriteRule ^b\.php(.*) /b.php$1
RewriteRule ^.*         /a.php
RewriteBase /
RewriteRule ^a\.php(.*)                          /a.php$1
RewriteRule ^b\.php(.*)                          /b.php$1
RewriteRule ^(.*)\.(js|ico|txt|gif|jpg|png|css)$ /$1.$2
基本上,重写规则是正则表达式和$1,$2,。。。是匹配字符串组(用“(“and”)包装的字符串组)


希望对您有所帮助。

实际上,您可以交换第二条规则和第三条规则,因为第二条规则将是默认路径:

# 1st
RewriteRule ^(a\.php|b\.php)$ - [L]

# 3rd
RewriteRule \.(js|ico|txt|gif|jpg|png|css)$ - [L]

# 2nd
RewriteRule !^a\.php$ a.php [L]

替代
-
意味着URI没有更改。

实际上,您可以交换第二条规则和第三条规则,因为第二条规则是默认路由:

# 1st
RewriteRule ^(a\.php|b\.php)$ - [L]

# 3rd
RewriteRule \.(js|ico|txt|gif|jpg|png|css)$ - [L]

# 2nd
RewriteRule !^a\.php$ a.php [L]

替代
-
意味着URI没有改变。

看起来不错。别忘了“重新启动引擎”。此外,定义这些规则的上下文(Config root、.htaccess)也很重要。我觉得很好。别忘了“重新启动引擎”。此外,定义这些规则的上下文(Config root、.htaccess)也很重要。+1对于两个答案,我选择Gumbo的答案是因为我更了解规则,谢谢两个答案。+1对于两个答案,我选择Gumbo的答案是因为我更了解规则,谢谢两个答案。。