【问题标题】:.htaccess to beautify a url.htaccess 美化网址
【发布时间】:2022-02-06 07:46:25
【问题描述】:

有几篇关于此的文章,但我无法使其发挥作用。
我有以下网址:

 1. /index.php
 2. /index.php?lg=el
 3. /index.php?lg=en
 4. /index.php?lg=el#anchor
 5. /index.php?lg=en#anchor
 6. /index.php?ct=1&lg=el
 7. /index.php?ct=1&lg=el#anchor
 8. /index.php?ct=1&lg=en
 9. /index.php?ct=1&lg=en#anchor
10. /index.php?ct=2&lg=el
11. /index.php?ct=2&lg=el#anchor
12. /index.php?ct=3&lg=el
13. /index.php?ct=3&lg=el#anchor
14. /index.php?ct=4&lg=el
15. /index.php?ct=4&lg=el#anchor
16. /index.php?ct=5&lg=el
17. /index.php?ct=5&lg=el#anchor
18. /index.php?ct=2&lg=en
19. /index.php?ct=2&lg=en#anchor
20. /index.php?ct=3&lg=en
21. /index.php?ct=3&lg=en#anchor
22. /index.php?ct=4&lg=en
23. /index.php?ct=4&lg=en#anchor
24. /index.php?ct=5&lg=en
25. /index.php?ct=5&lg=en#anchor

和类似的没有“index.php”。 “ct”变量应根据以下数组进行转换(索引从 1 开始):

[company,history,news,measurements,contact]

我想将它们转换为:

 1. /
 2. /el
 3. /en
 4. /el#anchor
 5. /en#anchor
 6. /el/company
 7. /el/company#anchor
 8. /en/company
 9. /en/company#anchor
10. /el/history
11. /el/history#anchor
12. /el/news
13. /el/news#anchor
14. /el/measurements
15. /el/measurements#anchor
16. /el/contact
17. /el/contact#anchor
18. /en/history
19. /en/history#anchor
20. /en/news
21. /en/news#anchor
22. /en/measurements
23. /en/measurements#anchor
24. /en/contact
25. /en/contact#anchor

我在 .htaccess 中尝试了以下语法:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\/)?([a-z]{2})?(\/)?((?:company|history|news|measurements|contact))?(#[\w\-]+)?$ $1index.php?ct=$4&lg=$2$5 [L,QSA]

但我无法得到预期的结果。
有什么想法吗?

【问题讨论】:

    标签: apache .htaccess url mod-rewrite url-rewriting


    【解决方案1】:
    6. /el/company
    7. /el/company#anchor
    

    #anchor(又名片段标识符)不会传递给服务器,因此它不是 URL 重写过程的一部分。 #anchor 由客户端代码使用,它存在于客户端 URL 上,因此在这方面没有什么可做的。因此,#6 和 #7(以及其他类似的 URL)在服务器请求方面实际上是“相同的”。

    诀窍是将第二个路径段转换为数字 1..5,而您在尝试中似乎没有这样做。

    如果您对始终存在ct URL 参数感到满意(除了对根的请求),但在不需要时(即ct=为空,而不是完全存在缺席,那么您可以在一条规则中执行以下操作:

    DirectoryIndex index.php
    
    RewriteEngine On
    
    # HTTP to HTTPS redirect
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    # Rewrite "pretty" URL
    RewriteCond 1$2 ^(\d)company [OR]
    RewriteCond 2$2 ^(\d)history [OR]
    RewriteCond 3$2 ^(\d)news [OR]
    RewriteCond 4$2 ^(\d)measurements [OR]
    RewriteCond 5$2 ^(\d)contact [OR]
    RewriteCond $2 ^$
    RewriteRule ^(el|en)(?:/(company|history|news|measurements|contact))?$ index.php?ct=%1&lg=$1 [L]
    

    由于您似乎只有两种语言,因此我使用了简单的交替来匹配这两种语言。 IE。 (el|en)。如果您确实有许多语言代码,请考虑将其更改为 [a-z]{2}。但是,最好是具体的并列出语言以避免潜在的冲突。

    由于您没有/en/contact 形式的文件,因此无需进行文件系统检查(检查请求是否映射到文件)。如果你这样做了,那么文件系统检查将无法进行重写。

    这里不需要RewriteBase 指令。

    第二个路径段对应的“数字”(数组索引)是从前面的 RewriteCond 指令中捕获的,稍后可以在 substitution 字符串中使用 %1 反向引用。

    上述规则将重写请求如下(非常接近您的示例):

    • / -> index.php(通过 mod_dir,无 URL 参数)
    • /el -> index.php?ct=&lg=elct 参数为
    • /el/(带有斜杠)- 不重写
    • /el/company -> index.php?ct=1&lg=el
    • /en/contact -> index.php?ct=5&lg=en
    • /ab - 不重写(无法识别的语言代码)
    • /en/contacts - 不重写(无法识别第二个路径段)

    和没有“index.php”的类似的。

    尽管今后,重写后的 URL 应始终包含 index.php

    我想将它们转换为:

    为了澄清,此规则将“漂亮”的 URL 重写为您的应用程序可以理解的基础 URL。它不会实际更改应用程序中使用的 URL - 您必须手动将 HTML 源中的 URL 更改为新的“漂亮” URL。

    但是,如果您正在更改已被搜索引擎索引并可能被第三方链接的现有 URL 结构,那么您还应该考虑实施从旧 URL 到新的“漂亮” URL 的外部重定向为了保持搜索引擎优化。但这并不是您的应用程序使用新的“漂亮” URL 所必需的。

    【讨论】:

    • 您的代码按您所说的那样工作,但我想知道是否有办法在 ct 参数为空或将其设置为 0 时将其删除。此外,除了您的代码已经执行的操作之外,还应该使用什么代码被添加到完全相反的过程中,这样只有经过美化的 url 才会出现在用户面前?此外,当有斜杠时,重写也应该与它之前的任何内容一起工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 2014-10-14
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    相关资源
    最近更新 更多