Mod重写了对index.php的大多数请求

时间:2022-04-02 11:22:19
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]

Above you can see my attempts to redirect any request that is not an existing directory, is not index.php and is not a static resource to redirect to index.php?p=resourcename

上面你可以看到我尝试重定向任何不是现有目录的请求,不是index.php而不是重定向到index.php的静态资源?p = resourcename

I am not having any luck. Basically the purpose of this is to redirect static and old urls to new ones as I have just rewritten an old site.

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

The PHP will handle the redirect logic.

PHP将处理重定向逻辑。

At the moment this code causes an internal server error, I assume because it is caught in a redirect loop. What have I done wrong? My brain is fried after a long day.

此代码导致内部服务器错误,我假设它是在重定向循环中捕获的。我做错了什么?经过漫长的一天,我的大脑被炒了。

3 个解决方案

#1


Untested, but worth a try :

未经测试,但值得一试:

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

The ".*" part means you want to match 1 or more characters (any of them). And the "/" in front of the "index.php" is probably not mandatory but makes things clearer even if you have the RewriteBase option set to "/" already.

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

You may also want to add the parameter "QS" between the brackets, to be sure to get the querystring that may be passed with the queries (would be [QS,R=301,L]). Hope this works, and this helps :)

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

Edit: There's also the "%" in front of "{REQUEST_FILENAME}", as stated by Gumbo.

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

#2


This code eventually solved my problem.

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

Thanks for the help, though.

谢谢你的帮助。

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]

#3


You forgot the % in front of the variable (%{REQUEST_FILENAME}) and an expression that should be repeated zero or more times (* is just a quantifier):

你忘了变量前面的%(%{REQUEST_FILENAME})和一个应该重复零次或多次的表达式(*只是一个量词):

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

#1


Untested, but worth a try :

未经测试,但值得一试:

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

The ".*" part means you want to match 1 or more characters (any of them). And the "/" in front of the "index.php" is probably not mandatory but makes things clearer even if you have the RewriteBase option set to "/" already.

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

You may also want to add the parameter "QS" between the brackets, to be sure to get the querystring that may be passed with the queries (would be [QS,R=301,L]). Hope this works, and this helps :)

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

Edit: There's also the "%" in front of "{REQUEST_FILENAME}", as stated by Gumbo.

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

#2


This code eventually solved my problem.

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

Thanks for the help, though.

谢谢你的帮助。

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]

#3


You forgot the % in front of the variable (%{REQUEST_FILENAME}) and an expression that should be repeated zero or more times (* is just a quantifier):

你忘了变量前面的%(%{REQUEST_FILENAME})和一个应该重复零次或多次的表达式(*只是一个量词):

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