2009-07-20 80 views
1
Options +FollowSymlinks 
RewriteEngine On 
RewriteBase/

##RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !\.(js|ico|gif|jpg|png|css)$ [NC] 
RewriteCond %{REQUEST_FILENAME} !^index.php [NC] 
RewriteCond %{REQUEST_FILENAME} !^$ [NC] 
RewriteCond %{REQUEST_FILENAME} !-d 

## RewriteRule ^/([a-zA-Z_+-]+).php$ index.php?p=%1 [R=301] 
## RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?p=$1 [R=301,L] 
## RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php?b={REQUEST_FILENAME} 
RewriteRule * index.php?p={REQUEST_FILENAME} [R=301,L] 

上面可以看到我尝试重定向不是现有目录中的任何要求,是不是和的index.php是不是一个静态的资源重定向到index.php 2 P = resourcename国防部重写大部分请求到index.php

我没有任何运气。基本上这样做的目的是将静态和旧的URL重新定向到新的URL,因为我刚刚重写了一个旧网站。

PHP将处理重定向逻辑。

此刻此代码会导致内部服务器错误,因此我认为它是在重定向循环中捕获的。我做错了什么?经过漫长的一天后,我的大脑被炸开了。

回答

1

这段代码最终解决了我的问题。

感谢您的帮助,但。

Options +FollowSymlinks 
RewriteEngine On 
RewriteBase/

RewriteCond %{REQUEST_FILENAME} !index.php 
RewriteRule ^/?([-a-zA-Z0-9_+]+).php$ /$1 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?p=$1 [L] 
0

您在变量(%{REQUEST_FILENAME}),并应重复零次或多次表达的前忘了%*仅仅是一个量词):

RewriteRule .* index.php?p=%{REQUEST_FILENAME} [R=301,L] 
+0

谢谢,现在纠正了。它仍然是坏的... – 2009-07-20 14:23:14

2

未经检验的,但值得一试:

RewriteRule .* /index.php?p={REQUEST_FILENAME} [R=301,L] 

“。*”部分表示您希望匹配1个或多个字符(它们中的任何一个)。并且“index.php”前面的“/”可能不是强制性的,但即使您已将RewriteBase选项设置为“/”,也会使事情更加清晰。

您可能还想在括号之间添加参数“QS”,以确保获得查询可能传递的查询字符串(将为[QS,R = 301,L])。希望这有效,这有助于:)

编辑:也有“{REQUEST_FILENAME}”前面的“%”,正如Gumbo所述。

+0

传递查询字符串的标志是`QSA` – jason 2009-07-20 14:33:16