gpt4 book ai didi

.htaccess 友好的 URL

转载 作者:行者123 更新时间:2023-12-04 23:59:16 25 4
gpt4 key购买 nike

有人可以帮我重写一些 URL 吗?

我有:
(例子)

www.example.com/index.php?page=namepage
www.example.com/index.php?page=gallery&topic=nametopic
www.example.com/index.php?page=homepage&paging=1

我想拥有:
www.example.com/namepage
www.example.com/gallery/nametopic
www.example.com/homepage/1

我的 htaccess 文件中有:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ?page=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)?/?$ ?page=$1&topic=$2

但效果不是很好,因为我可以写:
  • www.example.com/index.php?page=namepage(页面或其他)
  • www.example.com/?page=namepage(页面或其他)
  • www.example.com/namepage/
  • www.example.com/namepage(这是我想要的 - 没有其他)

  • 第二个问题是:
  • www.example.com/namepage(好吧,我想要,我们看到了姓名页)
  • www.example.com/namepage/whatever(不行,我想要 404,但我们看到了名称页)
  • www.example.com/gallery/topic(好吧,我想要,我们看到nametopic)
  • www.example.com/whatever/whatever2/whatever3(好吧,我想要 404)

  • 非常感谢任何人。

    最佳答案

    ### all your redirects

    # for www.example.com/index.php?page=homepage&paging=1
    RewriteCond %{THE_REQUEST} \?page=([^&]+)&paging=([0-9]+)
    RewriteRule ^ /%1/%2? [L,R=301]

    # for www.example.com/index.php?page=gallery&topic=nametopic
    RewriteCond %{THE_REQUEST} \?page=([^&]+)&topic=([^&\ ]+)
    RewriteRule ^ /%1/%2? [L,R=301]

    # for www.example.com/index.php?page=namepage
    RewriteCond %{THE_REQUEST} \?page=([^&\ ]+)($|\ )
    RewriteRule ^ /%1? [L,R=301]

    # for www.example.com/namepage/
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    ### all your rewrites back

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&paging=$2 [L]

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

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)$ /index.php?page=$1 [L]

    关于.htaccess 友好的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12538466/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com