【问题标题】:htaccess add .html extension for urls with or without trailing slashhtaccess 为带有或不带有斜杠的 url 添加 .html 扩展名
【发布时间】:2013-09-12 00:12:30
【问题描述】:

所以首先我有一个自定义 url 重写,它将请求变量发送到 php 脚本

重写规则如下:

RewriteRule ^([\w\/-]+)(\?.*)?$ test/index.php?slug=$1 [L,T=application/x-httpd-php]

因此,如果您访问 domain.com/slug-text 之类的内容,它会将 slug-text 发送到位于名为 test 的文件夹中的 index.php

我希望我的所有网址看起来像 domain.com/slug-text.html,但 slug-test 变量仍应发送到 index.php 文件。

我想不通的是重定向。我希望将所有旧网址从 domain.com/slug-textdomain.com/slug-text/ 重定向到 domain.com/slug-text.htmlslug-text 发送到位于 test 文件夹中的 index.php 文件。

搜索了很多,但在互联网上的任何地方都找不到这个问题的答案。

谢谢大家的帮助。

更新: 我的新代码是:

RewriteEngine On
Options +FollowSymlinks

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/\-]+)?[\w-])(?!:\.html)$ http://domain.com/$1\.html [L,R=301]
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]

domain.com/slug-text/ 不会被重定向到 domain.com/slug-text.html domain.com/slug-text 按预期工作,重定向到 domain.com/slug-text.html

我需要改变什么?

【问题讨论】:

    标签: php apache .htaccess redirect mod-rewrite


    【解决方案1】:

    这条规则:

    RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
    

    将捕获domain.com/slug-textdomain.com/slug-text/domain.com/slug-text.html 并将slug-text 发送到/test/index.php 内的slug 参数。

    如果您真的想使用 [R=301] 从旧网址重定向到新网址,请使用:

    RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
    RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
    

    另请注意,使用显式重定向底部规则被修改为捕获以.html结尾的url

    还建议(如果您的 .htaccess 尚未包含此内容)过滤现有文件和文件夹的条件,以免被重定向规则捕获。只需在RewriteRule 行之前添加这些行:

    # existing file
    RewriteCond %{SCRIPT_FILENAME} !-f
    # existing folder
    RewriteCond %{SCRIPT_FILENAME} !-d
    

    如果使用符号链接:

    # enable symlinks
    Options +FollowSymLinks
    # existing symlink
    RewriteCond %{SCRIPT_FILENAME} !-l
    

    // 加法
    您的 .htaccess 文件应如下所示:

    RewriteEngine on
    Options +FollowSymLinks
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-l
    RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
    RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
    

    【讨论】:

    • 您需要在domain.com 前面添加http://,否则apache 会在url 前面加上/path/to/webserver/root,因此您的重定向网址可能看起来像domain.com/var/www/domain.com/some-slug.html,它会生成404 Not Found
    • 您的 .htaccess 中有 RewriteEngine on 吗?它必须在任何RewriteRuleRewriteCond 之前
    • 在主要问题的更新中粘贴了我的整个htaccess,非常感谢您的帮助
    • 我的 htaccess 中的代码与您发布的方式相同,一切都按预期工作,只是末尾带有斜杠的 url 不会被重定向到我希望它们成为的 .html
    • @Jay 你的意思是domain.com 根据规则动态改变。它可以是半动态的。您可以创建两个或更多 RewriteRule 每个重定向到不同的域。我认为它可以是(虽然我还没有测试过)完全动态的,但我不会比使子域动态更进一步:RewriteRule ^(\w+)/?(?!:\.html)l$ http://$1.domain.com/$2.html [L,R=301]
    【解决方案2】:

    这应该将 /slug-text 重定向到 /slug-text.html

    RedirectMatch ^/([\w-]+)/?$ http://domein.com/$1.html 
    

    当 slug-text 仅包含字母、数字、- 和 _ 时,就会出现这种情况。 将 slug-text.html 重写为 php 文件并将 slug 作为参数传递:

    RewriteRule ^([\w-]+)\.html$ test/index.php?slug=$1 [R,L] 
    

    如果您的 .htaccess 中有两行,第一行将执行从旧 URL 到新 URL 的重定向,第二行将处理请求。

    【讨论】:

      猜你喜欢
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2019-01-26
      相关资源
      最近更新 更多