.htaccess为PHP取代URL

时间:2013-11-05 16:56:02

标签: php regex apache .htaccess mod-rewrite

默认为

/courses/category?cat=skills

/courses/course?cat=skills&course=timekeeping

我一直试图将上述内容缩短为

所需

/courses/skills

/courses/skills/timekeeping

catcourse有变量的时候使用以下作品

RewriteRule ^courses/([^/]*)/([^/]*)$ /courses/course?cat=$1&course=$2 [L]

我也尝试添加RewriteRule ^courses/([^/]*)$ /courses/category?cat=$1&course=$2 [L],但当我尝试访问/courses/skills时,它会出现:

内部服务器错误

注意:.php已使用

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

2 个答案:

答案 0 :(得分:1)

这应该是你完整的.htaccess:

RewriteEngine on

RewriteRule ^courses/([^/.]+)/?$ /courses/category?cat=$1 [L,QSA,NC]

RewriteRule ^courses/([^/.]+)/([^/.]+)/?$ /courses/course?cat=$1&course=$2 [L,QSA,NC]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

答案 1 :(得分:0)

RewriteRule ^courses/([^/]*)$ /courses/category?cat=$1&course=$2 [L]

在这里,$2没有任何意义,因为你没有定义它。

请改用:

RewriteRule ^courses/([^/]*)$ /courses/category?cat=$1 [L]