我一直看到 RewriteRule 用于公共 URL 路径,但我们也可以将 URL 映射到内部路径吗?
例如,将所有链接重定向到 my_page.php,是否允许?:
RewriteRule .* /home/yccaucom/public_html/my_page.php [last,noescape]
我一直看到 RewriteRule 用于公共 URL 路径,但我们也可以将 URL 映射到内部路径吗?
例如,将所有链接重定向到 my_page.php,是否允许?:
RewriteRule .* /home/yccaucom/public_html/my_page.php [last,noescape]
您不能重定向到不在域根目录中的文件夹或文件。
鉴于您的根文件夹是:
/home/yccaucom/public_html/
您只能重定向到 中的内容public_html
,例如public_html/css
或public_html/some_folder
,您还可以创建指向内部文件夹或文件的符号链接,它应该也可以工作。
鉴于您的规则,它看起来像这样:
RewriteCond %{REQUEST_URI} !^/my_page.php$ [NC]
RewriteRule ^ /my_page.php [L,NE]
您希望条件避免它陷入无限循环。
或者您可以使用以下 2 个条件检查现有文件/文件夹:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
基本上它说如果文件或文件夹不存在重定向。
如果您尝试按原样使用您的规则,它将尝试重定向到:
/home/yccaucom/public_html/home/yccaucom/public_html/my_page.php
但是它很可能不会失败,因为您没有任何验证来停止循环,因此它会给您一条失败消息,说明重定向太多左右。
在更深入地了解规则时,会发生以下情况:
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (3) [perdir /home/account/public_html/] strip per-dir prefix: /home/account/public_html/asdasdas2 -> asdasdas2
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (3) [perdir /home/account/public_html/] applying pattern '.*' to uri 'asdasdas2'
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (2) [perdir /home/account/public_html/] rewrite 'asdasdas2' -> '/home/account/public_html/edit.php'
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (2) [perdir /home/account/public_html/] trying to replace prefix /home/account/public_html/ with /
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (5) strip matching prefix: /home/account/public_html/edit.php -> edit.php
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (4) add subst prefix: edit.php -> /edit.php
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (1) [perdir /home/account/public_html/] internal redirect with /edit.php [INTERNAL REDIRECT]
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe852858798/initial/redir#1] (3) [perdir /home/account/public_html/] strip per-dir prefix: /home/account/public_html/edit.php -> edit.php
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe852858798/initial/redir#1] (3) [perdir /home/account/public_html/] applying pattern '.*' to uri 'edit.php'
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe852858798/initial/redir#1] (2) [perdir /home/account/public_html/] rewrite 'edit.php' -> '/home/account/public_html/edit.php'
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe852858798/initial/redir#1] (1) [perdir /home/account/public_html/] initial URL equal rewritten URL: /home/account/public_html/edit.php [IGNORING REWRITE]