.htaccess重写规则给出服务器错误

时间:2013-05-29 11:17:25

标签: .htaccess

我想我只需要第二双眼睛,因为我无法理解为什么我会收到服务器错误。

RewriteEngine On
RewriteRule ^Gig/([a-zA-Z0-9_-]+)$ gig.php
RewriteRule ^Gig/([a-zA-Z0-9_-]+)/$ gig.php
#allow non caps
RewriteRule ^gig/([a-zA-Z0-9_-]+)$ gig.php
RewriteRule ^gig/([a-zA-Z0-9_-]+)/$ gig.php

修改:

我现在查看了日志,原因是内部重定向太多了。我自己在mod_rewrites等方面不太称职,所以请看看。

#redirect so home page shows /Home
Redirect /home.php http://localhost/Home

#add php extension
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

Redirect /home.php http://localhost/Home

# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.*)\.php /$1 [R=301,L]

#redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.^([a-zA-Z0-9_-]+)$ [NC]
RewriteRule ^(.*)$ http://^([a-zA-Z0-9_-]+)$1 [L,R=301]

#remove trailing slash
RewriteEngine on 
RewriteRule ^(.*)/$ /$1 [L,R=301]

#allow artistprofile nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/?$ artist_profile.php

#info nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Aa]bout/?$ artist_about.php

#gigs nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Gg]igs/?$ artist_gigs.php

#tracks nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Tt]racks/?$ artist_tracks.php

#gig nice url
RewriteEngine On
RewriteRule ^[Gg]ig/([a-zA-Z0-9_\-]+)/?$ gig.php

这是.htaccess文件中的所有重写

修改: 问题是添加PHP扩展部分

#add php extension
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

我认为是因为([a-zA-Z0-9_-]+)位于文件名之后。

1 个答案:

答案 0 :(得分:1)

当你的-字符放在括号内时,你需要转义它们。您还可以将代码缩短为以下内容:

RewriteEngine On
RewriteRule ^[Gg]ig/([a-zA-Z0-9_\-]+)/?$ gig.php
# Escape it!                    ^^

为什么我们可以像这样缩短它?

  • [Gg]表示“字符Gg
  • /?表示“ /重复0或1次