.htaccess mod_rewrite导致500而不是404(无限内部重定向)

时间:2017-05-24 12:12:06

标签: php apache .htaccess redirect mod-rewrite

我的目标是从url中删除.php扩展名并创建语义URL多语言网站。这是我的.htaccess规则:

# Turn mod_rewrite on
RewriteEngine On

#Remove .php extensions from files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

#Create Semantic url
RewriteRule  ^event/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/?$  calendar?region=$1&news_id=$2 [L,NC]

#Handle language switch.
RewriteRule ^(ru)/(.*)$  $2?sitelang=2 [L,QSA]
RewriteRule ^(ru)$  $1?sitelang=2 [L,QSA]
RewriteRule ^(en)/(.*)$  $2?sitelang=1 [L,QSA]
RewriteRule ^(en)$  $1?sitelang=1 [L,QSA]
RewriteRule ^(.*)$  $1?sitelang=1 [L,QSA]

#404 Document /404.php
ErrorDocument 404 /404

所以我有一些情况下这个规则有效,但也有效。所以这是工作的:

  • domain.com/的 RU /事件/区域/一些事件名称
  • domain.com/event/region/some-event-name

那些不起作用的(当我添加一些内容时,粗体)会导致500错误:

  • domain.com/event 取值 /区域/一些事件名称
  • domain.com/event 取值 /区域/一些事件名/
  • domain.com/event/region/some-event-name/的东西

2 个答案:

答案 0 :(得分:1)

您需要停止现有文件和目录的规则执行。你有这样的规则:

#404 Document /404.php
ErrorDocument 404 /404

# Turn mod_rewrite on
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

#Remove .php extensions from files
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

#Create Semantic url
RewriteRule ^event/([\w-]+)/([\w-]+)/?$ calendar?region=$1&news_id=$2 [L,NC,QSA]

#Handle language switch.
RewriteRule ^(ru)/(.*)$  $2?sitelang=2 [L,QSA]
RewriteRule ^(ru)$  $1?sitelang=2 [L,QSA]
RewriteRule ^(en)/(.*)$  $2?sitelang=1 [L,QSA]
RewriteRule ^(en)$  $1?sitelang=1 [L,QSA]
RewriteRule ^(.*)$  $1?sitelang=1 [L,QSA]

答案 1 :(得分:0)

感谢@anubhava指出我正确的方向。我能够在你的帮助下解决我的问题。这是工作解决方案。

#404 Document /404.php
ErrorDocument 404 /404.php

# Turn mod_rewrite on
RewriteEngine On

#Remove .php extensions from files
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ - [L]

#Create Semantic url
RewriteRule ^event/([\w-]+)/([\w-]+)/?$ calendar?region=$1&news_id=$2 [L,NC,QSA]

#Handle language switch.
RewriteRule ^(ru)/(.*)$  $2?sitelang=2 [L,QSA]
RewriteRule ^(en)/(.*)$  $2?sitelang=1 [L,QSA]
RewriteRule ^(.*)$  $1?sitelang=1 [L,QSA]