如何在不破坏图像 CSS 和 JavaScript 的情况下实现友好的 URL?
How to implement friendly URLs without breaking images, CSS and JavaScript?
我站的链接显示如下:
blog.php?id=Blizzard
如何将它们转换成如下所示?
blog/Blizzard
使用 .htaccess
或 PHP?
我在 .htaccess
中使用了以下指令:
RewriteRule ^blog/(.*)$ ./blog.php?id=
但是图像CSS和JavaScript被破坏了!?
您可以使用 .htaccess 创建短网址。
示例:
RewriteEngine On
RewriteBase /
RewirteRule sample /sample2.php
只需要 3 行
RewriteEngine On <== Use Rewrite Module
RewriteBase / <== .htaccess file locate
RewriteRule <== ShortURL RULE
你写
RewirteRule sample /sample2.php
重定向 sample2.php
But the images , css and javascript codes are destroyed
可能是因为您对静态资源使用的是相对 URL。如果您将图像引用为 img/myimage.jpg
,那么 浏览器 现在将查找 /blog/img/myimage.jpg
,而不是像以前那样查找 /img/myimage.jpg
(使用您的示例) .
您需要使用 root-relative URLs(以斜杠开头)或绝对 URLs(方案 + 主机名)才能从不同的 URL 访问资源]-路径深度。有时您可以使用 head
部分中的 base
元素来解决此问题,但是,这种方法有一些注意事项。
见my answer to the following question on the Webmasters stack that goes into great detail on this exact issue: .htaccess rewrite URL leads to missing CSS
RewriteRule ^blog/(.*)$ ./blog.php?id=
您应该删除 RewriteRule
替换 上的 ./
前缀。点(表示“当前目录”)在 URL 路径中没有任何作用(它被解析掉了)。
如果您以后添加其他规则,您还应该包括 L
标志。
您还需要确保禁用 MutliViews,否则,blog.php
将在没有 id
属性的情况下“神奇地”调用,您的脚本将失败。 (MultiViews 基本上启用了无扩展名 URLs 但可能会导致其他问题。)参见 https://httpd.apache.org/docs/2.4/content-negotiation.html
可选地,如果您要更改 URL 结构,您还可以将 /blog.php?id=<value>
形式的 URL 重定向到 /blog/<value>
以保留 SEO 和重定向书签链接。 (但请注意,您应该已经在整个过程中链接到 /blog/<value>
形式的 URL。)
例如
# Disable MultiViews
Options -MuliViews
# Redirect "/blog.php?id=<value>" to "/blog/<value>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^id=([^&]*)
RewriteRule ^blog\.php$ /blog/%1 [QSD,R=301,L]
# Rewrite "blog/<value>" to "/blog.php?id=<value>"
RewriteRule ^blog/(.*) blog.php?id= [QSA,L]
关于上述指令的补充说明:
- 针对
REDIRECT_STATUS
环境变量的检查确保只有直接请求被重定向,而不是被随后的重写重写的请求。
QSD
标志从请求中丢弃原始查询字符串。
QSA
标志附加(合并)可能在原始请求上的任何查询字符串与在替换中分配的查询字符串。如果您不需要任何查询字符串,请删除此标志。例如。仅当存在 QSA
标志时,对 /blog/Blizzard?foo=1
的请求才会在内部重写为 /blog.php?id=Blizzard&foo=1
。
我站的链接显示如下:
blog.php?id=Blizzard
如何将它们转换成如下所示?
blog/Blizzard
使用 .htaccess
或 PHP?
我在 .htaccess
中使用了以下指令:
RewriteRule ^blog/(.*)$ ./blog.php?id=
但是图像CSS和JavaScript被破坏了!?
您可以使用 .htaccess 创建短网址。
示例:
RewriteEngine On
RewriteBase /
RewirteRule sample /sample2.php
只需要 3 行
RewriteEngine On <== Use Rewrite Module
RewriteBase / <== .htaccess file locate
RewriteRule <== ShortURL RULE
你写
RewirteRule sample /sample2.php
重定向 sample2.php
But the images , css and javascript codes are destroyed
可能是因为您对静态资源使用的是相对 URL。如果您将图像引用为 img/myimage.jpg
,那么 浏览器 现在将查找 /blog/img/myimage.jpg
,而不是像以前那样查找 /img/myimage.jpg
(使用您的示例) .
您需要使用 root-relative URLs(以斜杠开头)或绝对 URLs(方案 + 主机名)才能从不同的 URL 访问资源]-路径深度。有时您可以使用 head
部分中的 base
元素来解决此问题,但是,这种方法有一些注意事项。
见my answer to the following question on the Webmasters stack that goes into great detail on this exact issue: .htaccess rewrite URL leads to missing CSS
RewriteRule ^blog/(.*)$ ./blog.php?id=
您应该删除 RewriteRule
替换 上的 ./
前缀。点(表示“当前目录”)在 URL 路径中没有任何作用(它被解析掉了)。
如果您以后添加其他规则,您还应该包括 L
标志。
您还需要确保禁用 MutliViews,否则,blog.php
将在没有 id
属性的情况下“神奇地”调用,您的脚本将失败。 (MultiViews 基本上启用了无扩展名 URLs 但可能会导致其他问题。)参见 https://httpd.apache.org/docs/2.4/content-negotiation.html
可选地,如果您要更改 URL 结构,您还可以将 /blog.php?id=<value>
形式的 URL 重定向到 /blog/<value>
以保留 SEO 和重定向书签链接。 (但请注意,您应该已经在整个过程中链接到 /blog/<value>
形式的 URL。)
例如
# Disable MultiViews
Options -MuliViews
# Redirect "/blog.php?id=<value>" to "/blog/<value>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^id=([^&]*)
RewriteRule ^blog\.php$ /blog/%1 [QSD,R=301,L]
# Rewrite "blog/<value>" to "/blog.php?id=<value>"
RewriteRule ^blog/(.*) blog.php?id= [QSA,L]
关于上述指令的补充说明:
- 针对
REDIRECT_STATUS
环境变量的检查确保只有直接请求被重定向,而不是被随后的重写重写的请求。 QSD
标志从请求中丢弃原始查询字符串。QSA
标志附加(合并)可能在原始请求上的任何查询字符串与在替换中分配的查询字符串。如果您不需要任何查询字符串,请删除此标志。例如。仅当存在QSA
标志时,对/blog/Blizzard?foo=1
的请求才会在内部重写为/blog.php?id=Blizzard&foo=1
。