首页 > 解决方案 > 尝试调试 .htaccess 文件

问题描述

我开始使用我的 PHP 网页的 mod_rewrite 函数

我非常兴奋地创建了我的第一个重写规则,但是当我添加到我的 .htaccess 文件时,所有网页都出现错误 500(不仅是我试图重定向的那个)。

这是我的文件:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^([^/]*)/(([^/]*)/)*$
RewriteCond %{REQUEST_FILENAME}index.php !-f
RewriteCond %{REQUEST_FILENAME}index.html !-f
RewriteCond %{REQUEST_FILENAME}index.htm !-f
RewriteRule ^/([a-zA-Z\-]+)\/([a-zA-Z\-]+)\/([0-9]+)$ ficha_jugador.php?idjugador=$3 [L]
RewriteRule (.*) /index.html [L]

我的新行是第 8 行,其余的默认在我的网络服务器中

我为第 8 行尝试了一些更简单的方法来测试

RewriteRule ^writetest\.html http://www.after5pc.net [QSA,L]

但它也以 500 错误结束,所以它似乎不是语法问题

最终,我想做的是改变(塞住我的网址)

http://cadistas1910.com/ficha_jugador.php?idjugador=1304
by
http://cadistas1910.com/alvaro-garcia-canto/alvaro-garcia/1304

大家可以提供什么帮助吗?非常感谢!

标签: apache.htaccessmod-rewriteurl-rewritingfriendly-url

解决方案


I believe last line of your .htaccess file could be the culprit, could you please try following once.

RewriteEngine On
RewriteBase /

RewriteRule ^([a-zA-Z\-]+)\/([a-zA-Z\-]+)\/([0-9]+)$ ficha_jugador.php?idjugador=$3 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]

I have added a condition there to rewrite to index.html if and only if Uri has non existing directory/file, why because it will create an infinite loop without a condition, since you are rewriting everything to index.html and everything(by regex) matched index.html also, hence loop and 500 internal error you are getting.

Also your rule to rewrite from http://localhost:80/alvaro-garcia-canto/alvaro-garcia/1304 TO http://localhost:80/ficha_jugador.php?idjugador=1304 have been changed into a single line above.

NOTE: To fix this either make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.


推荐阅读