htaccess的友好的URL重写重写、友好、htaccess、URL

2023-09-02 01:06:56 作者:假装开心

我真诚地道歉,如果这是一个重复。我已经看了这么多线程但没有似乎专门适用于我想要做的..出于某种原因,我只是有这样一个困难的时候周围环绕我的大脑的.htaccess重写规则!

I sincerely apologize if this is a duplicate. I've looked at so many threads but none seem to apply specifically to what I'm trying to do.. and for some reason, I just have such a difficult time wrapping my brain around .htaccess rewrite rules!

我购买了一个PHP脚本,并安装它之后,我创建了一个新的岗位上工作得很好。当然,除了,它表明:mydomain.com/post.php?id=7

I purchased a php script and after installing it, I create a new post and it works fine.. except of course, it shows: mydomain.com/post.php?id=7

我要留言我要的是后显示的文章的标题中的.com /

Want I want is to show the title of the post after the .com/

我知道有些人会建议使用一个帖子ID或日期或什么的,但我真的preFER它写成规定。所以,最终的结果将是:mydomain.com/this-is-my-first-post

I know some will advise to use a post id or a date or something, but I'd really prefer it to be written as stated. So the end result would be: mydomain.com/this-is-my-first-post

然后,我希望能够正确地调用这个页面。我认为这将在index.php页面(它调用post.php中页)都是可以做的?

I then want to be able to correctly call this page. I assume this would all be done in the index.php page (which calls the post.php page)?

我发现了线的index.php,目前调用后,它看起来是这样的:

I found the line in index.php that currently calls the post and it looks like this:

<a href="post.php?id=<?php echo $theArticle['artId']; ?>"><h1><?php echo $theArticle['title']; ?></h1></a>

是还我需要更新了什么?

Is that also what I would need to update?

感谢您的任何帮助。我真的试过,但我只是不编程思想挣扎的境地!

Thanks for any assistance. I really have tried but I am just not programming minded and struggling mightily!

推荐答案

你的问题是有点太广,但如果你愿意妥协,你可以改变网址是这样的:

Your question is a bit too broad, but if you are willing to compromise, you could change the url to something like:

mydomain.com/post/7/the-title-of-post-7

然后你只需要一个重写规则并没有改变,在数据库中检索code。

And then you would only need a rewrite rule and no changes in the database retrieving code.

在这种情况下,重写规则看起来是这样的:

In that case the rewrite rule would look something like:

RewriteRule ^post/(d+).*$ /post.php?id=$1
                  ^^^^^ capture the number to be used

当然,

您还需要改写为所需格式的所有链接。

You would of course also need to rewrite all links to the required format.