.htaccess 中的 mod_rewrite

发布于 2024-09-06 11:59:18 字数 452 浏览 4 评论 0原文

大家好,我怎样才能减少这个:

www.example.com/index.php?page=viewblog&category=awesome

归结为:

www.example.com/blog/awesome

上面列出了该类别中的所有博客文章,但我也希望在其末尾添加帖子标题,如下所示

www.example.com/index.php?page=viewblog&category=awesome&post=why-trees-are-green

:需要缩短为:

www.example.com/blog/awesome/why-trees-are-green

有什么想法吗?提前致谢 :)

Hey all, how can I reduce this:

www.example.com/index.php?page=viewblog&category=awesome

down to this:

www.example.com/blog/awesome

The above lists all of the blog posts in that category, but I also want scope for adding the title of the post on the end of it as well, like this:

www.example.com/index.php?page=viewblog&category=awesome&post=why-trees-are-green

And this needs to shorten down to:

www.example.com/blog/awesome/why-trees-are-green

Any ideas, anyone? Thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东走西顾 2024-09-13 11:59:18
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f    
RewriteRule blog/([^/]+)/?$ index.php?page=viewblog&category=$1 [L]
RewriteRule blog/([^/]+)/([^/]+)/?$ index.php?page=viewblog&category=$1&post=$2 [L]

该条件确保请求的 URL 不是您想要提供的实际文件(css、图像等)

然后,每个级别一个规则,第一个规则将重写
http://yourserver.com/blog/whatever
http://yourserver.com/index.php?page=viewblog&category=whatever

第二个将重写http://yourserver.com/blog/whatever/whenever > 到
http://yourserver.com/index.php?page=viewblog&category=whatever&post=whenever

如果您需要更多级别,请相应地添加更多规则。

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f    
RewriteRule blog/([^/]+)/?$ index.php?page=viewblog&category=$1 [L]
RewriteRule blog/([^/]+)/([^/]+)/?$ index.php?page=viewblog&category=$1&post=$2 [L]

The condition makes sure the requested URL is not an actual file, which you want to serve (css, images, etc.)

Then, one rule per level, the first one will rewrite
http://yourserver.com/blog/whatever to
http://yourserver.com/index.php?page=viewblog&category=whatever

The second one will rewrite http://yourserver.com/blog/whatever/whenever to
http://yourserver.com/index.php?page=viewblog&category=whatever&post=whenever

If you need more levels, add more rules accordingly.

也只是曾经 2024-09-13 11:59:18

像这样的东西吗?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/([^./]+)$ index.php?page=view$1&category=$2 [L]
RewriteRule ^([^./]+)/([^./]+)/([^./]+)$ index.php?page=view$1&category=$2&post=$3 [L]

Something like this?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/([^./]+)$ index.php?page=view$1&category=$2 [L]
RewriteRule ^([^./]+)/([^./]+)/([^./]+)$ index.php?page=view$1&category=$2&post=$3 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文