apache 在.htaccess中重写URL而不重定向

nhn9ugyo  于 2023-02-16  发布在  Apache
关注(0)|答案(1)|浏览(143)

我想重写我的网站上的一些路径。我不想重定向,只是简单地修改URL。这是我迄今为止基于this answer所做的尝试:

RewriteRule ^thankyou.html /thankyou/$1 [L]

然而,所有这些都是重定向到我的主页。我有几个路径我想全局重写。但我想我会先在我的/thankyou.html页面上测试它,让它工作。所以在这个例子中,我只是试图重写example.com/thankyou.htmlexample.com/thankyou
下面是完整的.htaccess文件:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www.example\.com [NC]
RewriteRule ^ https://www.example.com/$1 [L,R=301]

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

RewriteRule ^thankyou.html /thankyou/$1 [L]

<IfModule mod_expires.c>
  ExpiresActive On

 # Images
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType image/x-icon "access plus 1 year"

  # Video
  ExpiresByType video/webm "access plus 1 year"
  ExpiresByType video/mp4 "access plus 1 year"
  ExpiresByType video/mpeg "access plus 1 year"

  # Fonts
  ExpiresByType font/ttf "access plus 1 year"
  ExpiresByType font/otf "access plus 1 year"
  ExpiresByType font/woff "access plus 1 year"
  ExpiresByType font/woff2 "access plus 1 year"
  ExpiresByType application/font-woff "access plus 1 year"

  # CSS, JavaScript
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType text/javascript "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"

  # Others
  ExpiresByType application/pdf "access plus 1 year"
  ExpiresByType image/vnd.microsoft.icon "access plus 1 year"
</IfModule>

我以前在AWS Amplify上使用JSON完成过此操作,如下所示:

[
    {
        "source": "https://www.example.com/thankyou.html",
        "target": "https://www.example.com/thankyou",
        "status": "301",
        "condition": null
    }
]

但不幸的是,这个网络应用程序不是托管在Amplify上的,我正在努力寻找一个等效的。

3j86kqsm

3j86kqsm1#

RewriteRule ^thankyou.html /thankyou/$1 [L]

如果底层文件是thankyou.html,那么重写的方式就错了,应该将请求从/thankyou/(我假设这是预期的规范URL)重写为/thankyou.html,因此应该在HTML源代码中请求(链接到)URL /thankyou/

  • aside:**substitution * 字符串中的$1反向引用是多余的。在这个例子中它总是空的。

换句话说:

RewriteRule ^thankyou/$ thankyou.html [L]

内部重写不需要 * substitution * 字符串上的斜杠前缀(最好省略)。
请注意,由于URL路径也与文件基本名称匹配,因此您需要确保同时禁用"多视图"(mod_negotiation的一部分),否则您的规则基本上会被忽略(这可能会导致以后出现问题)。在.htaccess文件的顶部,您应该添加:

Options -MultiViews
    • 更新日期:**

这听起来像是你希望通过上面的操作来神奇地改变HTML源代码中的URL。事实并非如此,也不是.htaccess的目的。要改变用户看到的URL(即规范的URL),你必须实际上改变HTML源代码中链接到的URL-如果这是一个静态网站,就没有捷径可走。
如果"旧" URL(即/thankyou.html)是由搜索引擎建立和索引和/或由外部第三方链接到,那么您也可以包括一个外部301重定向从/thankyou.html/thankyou,以保持SEO。但请注意,这不是必要的,您的网站的功能,因为你应该已经改变了底层HTML源代码中的URL。2这对SEO来说是必须的。
如果您不更改HTML源代码中的URL(并且仍然实现"重定向"),则用户仍然可以在页面上"看到"旧的URL(当他们悬停或"复制"链接时),并且每次他们单击链接时,他们都被外部重定向(对服务器的请求数量加倍,并可能降低用户的速度)。
例如:

Options -MultiViews

# Redirect from "/thankyou.html" to "/thankyou" for SEO
RewriteRule ^thankyou\.html$ /thankyou [R=301,L]

# Rewrite from "/thankyou" to "/thankyou.html"
RewriteRule ^thankyou$ thankyou.html [END]

请注意,在第二个(重写)规则中添加了END标志。
还要注意的是,我已经删除了URL末尾的斜杠,因此,/thankyou是您应该链接到的URL,/thankyou.html是处理请求的底层文件。

相关问题