【问题标题】:htaccess seo friendly urls for all subfolders所有子文件夹的 htaccess seo 友好 URL
【发布时间】:2013-02-05 16:19:56
【问题描述】:

我在所有文件夹的 index.php 中都有脚本

需要像这样制作漂亮的 url/seo 友好的 url:site.com/folder1/something.html

现在我有这样丑陋的网址:site.com/folder1/index.php?category=something

这部分index.php?category=something 在所有子文件夹中始终相同,我只想将其更改为对 seo 更友好,例如 something.html

再次:找到 index.php?category=1$ 并将其替换为 1$.html 不要碰其他任何东西,只是这部分 url

所以当我访问时:

site.com/another-subfolder/and-one-more-folder-here/index.php?category=something

需要在地址栏中看到这个:

site.com/another-subfolder/and-one-more-folder-here/something.html

希望你明白吗?

我在 root 的 htaccess 中使用 folder1 尝试过这个

RewriteRule ^folder1/(.*).html$ folder1/index.php?category=$1 [L,R=301]

好的,但是我怎样才能使它适用于整个站点的所有子文件夹,如下所示:

site.com/folder145/index.php?category=something
site.com/subfolder/index.php?category=something
site.com/another-subfolder/and-one-more-folder-here/index.php?category=something

会有 1000 个不同名称的子文件夹,因此手动为每个子文件夹创建 RewriteRule 将不起作用

感谢任何帮助

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    首先,您需要将生成的所有链接从index.php?category= 表单更改为category.html 表单。

    然后在文档根目录的 htaccess 文件中,添加这些规则以在内部将 category.html 表单重写为 index.php?category= 表单:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+?/)?([^/]+)\.html$ /$1index.php?category=$2 [L]
    

    然后,指向您无法控制的链接(或使用 FORM 生成的链接)外部重定向对 index.php?category= 表单中链接的任何直接访问:

    RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ (/.*?)?/index\.php\?category=([^&\ ]+)
    RewriteRule ^(.+?/)?index\.php$ /$1%3.html? [L,R=301]  
    

    这应该匹配任何子目录。

    【讨论】:

    • yesssssss 终于成功了,非常感谢你,最近几天我试图解开这个谜,但我做不到,你是我的救星:) 这和我描述的完全一样! !!再次感谢:)
    猜你喜欢
    • 2011-05-29
    • 2013-01-22
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2011-05-09
    • 2015-01-13
    • 2019-11-08
    相关资源
    最近更新 更多