php RewriteRule 创建 500 内部服务器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17107940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:16:48  来源:igfitidea点击:

RewriteRule creating 500 Internal Server Error

phpapache.htaccessmod-rewrite

提问by Tom

I have the following in my .htaccess file:

我的 .htaccess 文件中有以下内容:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^directory/(.*)$ directory/index.php?id=

What I'm trying to achieve is this:

我想要实现的是:

When the URL www.mydomain.com/directory/10is visited, the page www.mydomain.com/directory/?id=10is displayed on the browser without altering the appearance of the URL.

当 URLwww.mydomain.com/directory/10被访问时,页面www.mydomain.com/directory/?id=10会显示在浏览器上,而不会改变 URL 的外观。

The above code creates a 500 Internal server error though.

上面的代码虽然创建了 500 内部服务器错误。

Does anyone know where I'm going wrong?

有谁知道我哪里错了?

回答by anubhava

Your code is guaranteed to generate 500 internal server error because it is causing infinite looping. Reason is that your matching URI pattern is: ^directory/(.*)$

您的代码保证会产生 500 个内部服务器错误,因为它会导致无限循环。原因是您匹配的 URI 模式是:^directory/(.*)$

Which matches your URLs before and after rewrites. And once it reaches max allowed internal rewrite limit Apache throws 500 internal server error and bails out.

在重写之前和之后匹配您的 URL。一旦达到最大允许的内部重写限制,Apache 就会抛出 500 个内部服务器错误并退出。

Change your code to this:

将您的代码更改为:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^directory/(.*)$ /directory/index.php?id= [L,QSA,NC]

Above code has an extra RewriteCond %{REQUEST_FILENAME} !-fthat will make sure to disallow subsequent execution of RewriteRule after first time since /directory/index.phpwill be a valid file.

上面的代码有一个额外的东西RewriteCond %{REQUEST_FILENAME} !-f,它将确保在第一次之后禁止 RewriteRule 的后续执行,因为/directory/index.php它将是一个有效的文件。

回答by Brilliant-DucN

I have got the same issue and found that "rewrite" module is not yet enabled in my case. So I need to enable it and then restart apache server:

我遇到了同样的问题,发现在我的情况下尚未启用“重写”模块。所以我需要启用它,然后重新启动 apache 服务器:

  • Enable "rewrite" module: sudo a2enmod rewrite
  • Then restart apache server: sudo service apache2 restart
  • 启用“重写”模块:sudo a2enmod rewrite
  • 然后重启apache服务器:sudo service apache2 restart

Hope this will help anyone.

希望这会帮助任何人。

回答by Paul S

You should try adding a forward slash to the front:

您应该尝试在前面添加一个正斜杠:

RewriteRule ^/directory/(.*)$ directory/index.php?id=

I've been caught out with that before.

我以前就被这件事搞糊涂了。

Alternatively use the RewriteLog and RewriteLogLevel to debug, and look at the Apache error and access logs for further info:

或者使用 RewriteLog 和 RewriteLogLevel 进行调试,并查看 Apache 错误和访问日志以获取更多信息:

RewriteLogLevel 3
RewriteLog ${APACHE_LOG_DIR}/rewrite.log

That will leave a log file in your apache log directory. In my case that is /var/log/apache

这将在您的 apache 日志目录中留下一个日志文件。在我的情况下/var/log/apache

回答by Davi Sousa

If you are using CodeIgniter and is in error problems 500. Follow the solution.

如果您正在使用 CodeIgniter 并且遇到错误问题 500。请遵循解决方案。

So to delete the segment "index.php" of URLs in CodeIgniter, you need to do 2 things. The first is to edit the /system/application/config/config.phpfile, changing the value of index_page policy to empty:

所以要在 CodeIgniter 中删除 URL 的“index.php”段,你需要做两件事。首先是编辑/system/application/config/config.php文件,将 index_page 策略的值更改为空:

$config['index_page'] = '';

The second step is to create a file .htaccess

第二步是创建文件 .htaccess

RewriteEngine on
RewriteCond  !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/ [L]

And that's it! From now on, the URLs of your site/system made with CodeIgniter will no longer have the thread (called "annoying" by some) "index.php".

就是这样!从现在开始,您使用 CodeIgniter 制作的站点/系统的 URL 将不再包含线程(被某些人称为“烦人”)“index.php”。