0

我在 localhost 中有一个使用缩短 URL 的站点

http://localhost/Portal/mysite/profile.php?id=1

http://localhost/Portal/mysite/profile/1/this_is_id

使用.htaccess下面的

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

RewriteBase /

RewriteRule ^profile/([0-9]+)/.*$  /Portal/mysite/profile.php?id=$1 [QSA,L,NC]

但是,我需要将 URL 改为http://localhost/Portal/mysite/this_is_id

这都在 localhost 中,这就是为什么.com没有出现,但是站点文件是mysite 所以,我尝试发送一个在我的mysite/index.phptomysite/profile.php中的链接,$id但它不起作用。无论如何简单的建议就可以了,谢谢

更新


好的,我按照你们俩的要求做了,但这是将用户从索引页面发送到个人资料页面的功能,我不知道如何修改它,即使我已经按照你们俩的要求做了。

    // sql query goes here. 
    foreach ($stmt as $row) {
    $url = "/profile/$row[id]/".preg_replace('/[^a-zA-Z0-9-_]/', '-', $row['company']);  

        echo "<h4><a href=\"$url\" class=\"urln\">". substr($row['company'], 0,26)."</a></h4>
                    ";

    }

回声是链接,现在当我按下该链接时,它会带我到未找到的页面

更新 2


在此处输入图像描述 这是我的目录的图像,链接在 index.php 中,当点击它时,它会将我发送到 profile.php 所有上述文件都位于WAMP/www/Portal/mysite

4

2 回答 2

1

试试这个:

#for subdirectory
RewriteBase /Portal/mysite/
#for localhost
#RewriteBase /
#RewriteRule ^profile/([0-9]+)$ profile.php?id=$1 [QSA,L,NC]
#RewriteRule ^profile/([0-9]+)/([a-zA-Z0-9\-]+)$ profile.php?id=$1 [QSA,L,NC]
RewriteRule ^profile/([a-zA-Z0-9\-]+)$ profile.php?id=$1 [QSA,L,NC]

.htaccessprofile.phpPortal/Site目录中。

更新:

foreach ($stmt as $row) {
    $url = "/profile/".preg_replace('/[^a-zA-Z0-9-_]/', '-', $row['company']) . "-" . $row[id];
    echo "<h4><a href=\"$url\" class=\"urln\">". substr($row['company'], 0,26)."</a></h4>";

}

profile.php

var_dump($_GET);

if(isset($_GET['id'])) {
    $params = explode('-', $_GET['id']);
    $id = (int)array_pop($params);

    var_dump($id);
}

.htaccess

RewriteEngine On
RewriteBase /Portal/mysite/
RewriteRule ^profile/([a-zA-Z0-9\-]+)$ profile.php?id=$1 [QSA,L,NC]

运行您的链接( fe: localhost/Portal/mysite/profile/This-is-name-12)并结果:

array (size=1) 'id' => string 'This-is-name-12' (length=15)

int 12

12是您的个人资料 ID。

于 2013-03-28T20:22:43.600 回答
0

尝试:

RewriteRule ^([0-9]+)$  profile.php?id=$1 [QSA,L,NC]

如果规则 ( .htaccess) 在您的/Portal/mysite/目录中,并且profile.php也在那里。

于 2013-03-28T19:54:10.123 回答