过去我使用 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/