帮助使用 .htaccess 重写 URL

发布于 2024-09-27 18:11:08 字数 3115 浏览 1 评论 0原文

我的服务器上有 html 静态文件。我希望访问规则为 -

  1. 如果用户访问 http://example.com/test/ 他应该得到文件内容http://example.com/test.html
  2. 如果用户访问http://example.com/test (没有尾部斜杠),他会被重定向到 http://example.com/test/ (运行规则 1 并获取文件 test.html 的内容)
  3. 规则 1 和 2 应该只如果文件 test.html 存在则触发。

到目前为止,我已经 -

Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off

<IfModule mod_rewrite.c>
DirectorySlash Off
RewriteEngine On
RewriteBase /

# If it's a request to index.html
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.html(\?.*)?\  [NC]
# Remove it.
RewriteRule ^(.+/)?index\.html$ /%1 [R=301,L]

# Add missing trailing slashes to directories if a matching .html does not exist.
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# If it's a request from a browser, not an internal request by Apache/mod_rewrite.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# And the request has a HTML extension. Redirect to remove it.
RewriteRule ^(.+)\.html$ /$1 [R=301,L]

# If the request exists with a .html extension.
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule ^(.*)/?$ $1.html [QSA,L]
</IfModule>

虽然它适用于 http://example.com/test,但它不适用于 http://example.com/test/ (500内部错误)

倒数第二行不应该处理尾随吗斜线?

[更新] 如果我使用 Gumbo 建议(后面是 .htaccess),则 http://example.com/testhttp://example.com/test/ (带有尾部斜杠)

Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off

ErrorDocument 404 /404.html

# Hide .htaccess 
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>

<IfModule mod_rewrite.c>
DirectorySlash On
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^([^/]+)/$ $1.html [L]

</IfModule>

[更新 2] 我已经放弃了。下面的代码可以工作,但它不会强制所有内容都以 / 结尾(http://example.com/test/),而是删除尾部斜杠(http://example.com/test)。好的一面是,它可以确保内容仅由一个 url 指向,从而保留 SEO。我现在要忍受它。

Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off

ErrorDocument 404 /404.html

# Hide .htaccess 
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>

<IfModule mod_rewrite.c>
DirectorySlash On
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]

# If the request exists with a .html extension.
RewriteCond %{SCRIPT_FILENAME}.html -f
# And there is no trailing slash, rewrite to add the .html extesion.
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]


</IfModule>

I have html static files on my server. I want the access rules as -

  1. If user visits http://example.com/test/ he should get the contents of the file http://example.com/test.html
  2. If the user visits http://example.com/test (without the trailing slash), he gets redirected to http://example.com/test/ (which runs rule 1 and gets him the contents of the file test.html)
  3. Rules 1 and 2 should only fire if the file test.html exists.

So far, I have -

Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off

<IfModule mod_rewrite.c>
DirectorySlash Off
RewriteEngine On
RewriteBase /

# If it's a request to index.html
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.html(\?.*)?\  [NC]
# Remove it.
RewriteRule ^(.+/)?index\.html$ /%1 [R=301,L]

# Add missing trailing slashes to directories if a matching .html does not exist.
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# If it's a request from a browser, not an internal request by Apache/mod_rewrite.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# And the request has a HTML extension. Redirect to remove it.
RewriteRule ^(.+)\.html$ /$1 [R=301,L]

# If the request exists with a .html extension.
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule ^(.*)/?$ $1.html [QSA,L]
</IfModule>

Although it works for http://example.com/test, it fails for http://example.com/test/ (500 internal error)

Shouldn't the second last line take care of trailing slashes?

[Update]
If I use Gumbo suggestions (.htaccess follows), I get a 404 for both http://example.com/test and http://example.com/test/ (with trailing slash)

Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off

ErrorDocument 404 /404.html

# Hide .htaccess 
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>

<IfModule mod_rewrite.c>
DirectorySlash On
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^([^/]+)/$ $1.html [L]

</IfModule>

[Update 2]
I have given up. The code below works, but instead of forcing everything end with a / (http://example.com/test/), it removes the trailing slash (http://example.com/test). On the bright side, it ensures that the content is pointed to by only one url, preserving SEO. I'm going to live with it for now.

Options All -Indexes -Multiviews
# Do not return details of server
ServerSignature Off

ErrorDocument 404 /404.html

# Hide .htaccess 
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>

<IfModule mod_rewrite.c>
DirectorySlash On
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]

# If the request exists with a .html extension.
RewriteCond %{SCRIPT_FILENAME}.html -f
# And there is no trailing slash, rewrite to add the .html extesion.
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]


</IfModule>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅 2024-10-04 18:11:08

尝试这些规则:

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^([^/]+)/$ $1.html [L]

Try these rules:

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^([^/]+)/$ $1.html [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文