简体   繁体   English

Apache PHP mod_rewrite无法正常工作

[英]Apache PHP mod_rewrite not working

I'm trying to test out the mod_rewrite meaning: I have two files: level1.php and warpzone.php . 我正在尝试测试mod_rewrite的含义:我有两个文件: level1.phpwarpzone.php

In my .htaccess I have the following code: 在我的.htaccess文件中,我有以下代码:

RewriteEngine On
RewriteRule ^warp-zone.php$ level-1.php

Yet when I go to warpzone.php, nothing happens :/ (the url doesn't change) 但是,当我转到warpzone.php时,什么也没发生:/(URL不变)

Any suggestions? 有什么建议么? I really don't know what I'm doing wrong. 我真的不知道我在做什么错。

First of all you have dashes in php file names at your .htaccess file, while your files does not have them. 首先,您的.htaccess文件中的php文件名中包含破折号,而文件中没有破折号。

Rewrite conditions - this changes the content to new one, but URL stays as it was: 重写条件-这会将内容更改为新内容,但URL保持不变:

RewriteEngine On
RewriteRule    ^warpzone\.php$  level1.php  [L]

Redirect conditions - this changes both content and URL to new one: 重定向条件-这会将内容和URL都更改为新的条件:

RewriteEngine On
RewriteRule    ^warpzone\.php$  /level1.php  [R]

Also you can use Redirect condition instead of mod_rewrite: 您也可以使用重定向条件代替mod_rewrite:

Redirect 301 /warpzone.php /level1.php

In case if it does not work for you, you can use headers() method at the top of your warpzone.php file: 如果它对您不起作用,则可以在warpzone.php文件顶部使用headers()方法:

<?php 
header("Location: " . $_SERVER['SERVER_NAME'] . "/level1.php");
?>
  • check whether warpzone.php or warp-zone.php is the correct filename, you are mixing them in your example. 检查warpzone.php或warp-zone.php是正确的文件名,请在示例中混用它们。
  • check the apache error.log file if mod_rewrite is available, if not then enable it (this can be done either with "LoadModule ..." or it is wrapped by your Linux distribution) 检查apache error.log文件(如果有mod_rewrite可用),如果没有,则将其启用(这可以通过“ LoadModule ...”完成,或者由Linux发行版包装)
  • check the apache error.log if you are allowed to use mod_rewrite, if not consider setting "AllowOverride All" in global config files httpd.conf/apache2.conf 如果允许使用mod_rewrite,请检查apache error.log,如果不考虑在全局配置文件httpd.conf / apache2.conf中设置“ AllowOverride All”
  • change your rewrite rule to "RewriteRule ^warp-zone.php$ level-1.php [L] " to issue an http redirect; 将您的重写规则更改为“ RewriteRule ^ warp-zone.php $ level-1.php [L] ”,以发出http重定向; without L flag, is is handled internally in the web sever; 没有L标志,则在Web服务器内部处理; see https://httpd.apache.org/docs/2.4/rewrite/flags.html for all flags 有关所有标志,请参见https://httpd.apache.org/docs/2.4/rewrite/flags.html

regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM