使用.htaccess重写多个URL

时间:2013-12-27 20:25:59

标签: php apache .htaccess mod-rewrite url-rewriting

我的.htaccess文件中有以下代码,位于“root / mosaicin /”目录中。

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /mosaicin/

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

RewriteRule ^(download-sample-papers|album|results|testimonials)/([^/]+)/?$ /$1.php?url=$2 [NC,QSA,L]

我想将我的网址www.domain.com/mosaicin/album.php/NID/重写为domain.com/mosaicin/album/NID/ 与规则中列出的其他页面相同(结果,推荐等)

我的album.php文件中的示例代码就像

<?php
$cid = NULL;
$url = new SimpleUrl('/mosaicin/');
if($url->segment(2) == FALSE)
    $cname = 'NID';
else
    $cname = $url->segment(2);
$cname = urldecode($cname);
$ccsql = "SELECT courseid,cname FROM courses WHERE cname='".$cname."'";
$csql = $obj->select($ccsql);
if(count($csql)>0)
$cid = $csql[0]['courseid'];
?>

获得courseid后,我的代码可以列出与特定课程相关的项目。 目前我去页面的时候 tplabs.in/mosaicin/album.php/NID一切正常,但链接如下 tplabs.in/mosaicin/album/NID它给出了404错误。

请帮忙, 提前谢谢。

1 个答案:

答案 0 :(得分:2)

DOCUMENT_ROOT/.htaccess文件中尝试以下规则:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /mosaicin/

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

RewriteRule ^(download-sample-papers|album|results|testimonials)/([^/]+)/?$ $1.php?url=$2 [NC,QSA,L]