.htaccess - 当您需要多个处理页面时,将 SEO URL 与 PHP 结合使用

标签 .htaccess url-rewriting seo

过去我使用 htaccess 来拦截对特定页面的请求,但我现在需要能够创建一堆 SEO 友好的 URL,这些 URL 将指向多个页面,我认为这可能不是那么简单。

例如:

www.domain.com/training
                  ^
        easy, as this can be pointed at a training.php page

www.domain.com/training/apple/final-cut-pro
                          ^
  trickier, since 'apple' may be dynamically created in the database,
  and the handling page for vendor training needs to be different to the 
  handling page for the specific course or the top level training page

所以在上面的示例中,我可能想将/training 指向 training.php,但将/training/apple 指向 vendors.php,将/training/apple/final-cut-pro 指向 products.php(可能还有更深层次的水平也)。除此之外,还将有/agency/xxx/xxx 和/news/xx 类型的链接。

htaccess 中有没有一种方法可以查看 URL 的第一部分(即“培训”、“新闻”),然后“计算”URL 的深度以确定应该将其发送到哪里?一种条件,如:

if root = 'training' then
    if depth = 1 then
        redirect to training.php
    else if depth = 2 then
        redirect to vendor.php
    else if depth = 3 then
        redirect to product.php
else if root = 'agency' then
    etc...

感谢您对此的任何帮助!

最佳答案

你可以这样做:

RewriteEngine On
RewriteBase /

# depth=1
RewriteRule ^training/?$ training.php [L,NC]

# depth=2
RewriteRule ^training/[^/]+/?$ vendor.php [L,NC]

# depth=3
RewriteRule ^training/[^/]+/[^/]+/?$ product.php [L,NC]

关于.htaccess - 当您需要多个处理页面时,将 SEO URL 与 PHP 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27525702/

相关文章:

PHP 获取 URL 作为变量

php - HTACCESS 问题导致 401 和多个 302

php - 语言重定向的 HTTP 状态码

seo - 从 Google 搜索结果中删除已删除的页面

php - 在 PHP 中建立 SEO URL 系统的好方法是什么?

apache - .htaccess 将访问 .html 链接的流量重定向到 TLD 主页

.htaccess AuthType 基本文件匹配 401 和 403 的除 Maintenance.html 之外的所有文件

具有重写规则的 Azure CDN

Javascript:重定向到不同的子域

php - 如何修改核心php中的链接