如何重写一个剧本,并从该脚本中使用的.htaccess同时避免无限循环重定向离开 [英] How to rewrite to a script and also redirect away from that script using .htaccess while avoiding infinite loops
问题描述
我想在我的网站通过一个脚本来处理所有的URL。所以,我把一个重写规则是这样的:
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则(。*)/myscript.php?p=$1 [L]
但我不希望允许访问我的脚本上的网址实际包含myscript.php在其中,所以我想这些重定向回主网站:
重定向301 /myscript.php http://example.com/
现在的问题是,如果我把这两方面的规则到我的.htaccess文件它会导致一个无限循环。我该如何让他们既可以在同一时间工作?
编辑:我也想能够重定向的东西,如:
/myscript.php?p=foo - > / FOO
编辑:下面是答案拼凑起来我的完整工作.htaccess文件。我把这个在测试/
目录现在,所有提及测试/应取出得到这个工作在根目录下。
RewriteEngine叙述上
DirectoryIndex的myscript.php
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{REQUEST_URI}!MyScript可\ .PHP
(。*)重写规则/test/myscript.php?p=$1 [L,E = LOOP:1]
的RewriteCond%{ENV:REDIRECT_LOOP} 1!
的RewriteCond%{QUERY_STRING} P =(。+)
重写规则^的MyScript \ .PHP $ /测试/%1? [R,L]
的RewriteCond%{ENV:REDIRECT_LOOP} 1!
重写规则^的MyScript \ .PHP $ /测试/ [R,L]
您可以设置环境变量
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{REQUEST_URI}!MyScript可\ .PHP
(。*)重写规则/myscript.php?p=$1 [L,E = LOOP:1]
和测试,在你的第二个规则
的RewriteCond%{ENV:REDIRECT_LOOP} 1!
重写规则^的MyScript \ .PHP $ / [R,L]
切勿 301
测试启用,看到这个答案提示调试的.htaccess重写规则了解详细信息。
I want to have all the URLs on my site handled by a single script. So I put in a rewrite rule like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /myscript.php?p=$1 [L]
But I don't want to allow access to my script on URLs that actually contain "myscript.php" in them so I would like to redirect those back to the main site:
Redirect 301 /myscript.php http://example.com/
The problem is that if I put both of those rules into my .htaccess file it causes an infinite loop. How do I get them both to work at the same time?
Edit: I would also like to be able to redirect things like:
/myscript.php?p=foo -> /foo
Edit: Here is my complete working .htaccess file cobbled together from the answer. I put this in a test/
directory for now, all references to "test/" should be taken out to get this working in the root directory.
RewriteEngine On
DirectoryIndex myscript.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !myscript\.php
RewriteRule (.*) /test/myscript.php?p=$1 [L,E=LOOP:1]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteCond %{QUERY_STRING} p=(.+)
RewriteRule ^myscript\.php$ /test/%1? [R,L]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^myscript\.php$ /test/ [R,L]
You can set an environment variable
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !myscript\.php
RewriteRule (.*) /myscript.php?p=$1 [L,E=LOOP:1]
and test for that in your second rule
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^myscript\.php$ / [R,L]
Never test with 301
enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
这篇关于如何重写一个剧本,并从该脚本中使用的.htaccess同时避免无限循环重定向离开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!