【问题标题】:Clean URLS www. to non, remove index.php, remove .php extention, add trailing slash using HTACCESS清洁网址 www。为非,删除 index.php,删除 .php 扩展名,使用 HTACCESS 添加斜杠
【发布时间】:2014-10-20 22:14:30
【问题描述】:

因此,我似乎可以为其中一些找到解决方案,但无法让它们一起工作。我想做的是从各个方面创建干净的 URL。

  1. 解决所有 www.和非万维网。到非 www。页面
  2. 删除所有出现的 index.php(即,如果导航到文件夹 /blog/index.php 解析为 /blog/)
  3. 从所有 URL 中删除 php 扩展名(即 /page.php 到 /page/)
  4. 添加尾部斜杠(即 /page 到 /page/)

这是我目前所拥有的:

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

这完成了干净的 URL 删除 .php 扩展名并添加尾部斜杠。我不得不取出 www.to non 并删除 index.php 因为干净的 url 和尾部斜杠停止工作。提前谢谢大家。

【问题讨论】:

  • 您确定 URL 仍然可以这样工作吗?似乎您想将用户从 ../page.php 重定向到 ../page/,但 Apache 会知道在哪里寻找 ../page/

标签: php apache .htaccess mod-rewrite


【解决方案1】:

你的.htaccess 应该是这样的:

RewriteEngine On

# Remove www.
<IfModule mod_rewrite.c>
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>

# Remove file extensions, add a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

This 是一篇关于从 URL 中删除文件扩展名的非常好的参考文章。请记住,要使其正常工作,您必须在所有链接中引用非扩展版本,例如&lt;a href="about"&gt;About&lt;/a&gt;,不是&lt;a href="about.php"&gt;About&lt;/a&gt;

当你在做.htaccess 的事情时,我可能还建议添加以下 sn-ps。前两个与网站速度有关,第二个用于自定义 404 页面,第三个用于强制UTF-8(因此您不必在 HTML 中声明)。

# Expires caching (Caching static files for longer drastically improves performance, you might even want to put even more aggressive times)
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>

# Gzip 
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>

# 404 Page
ErrorDocument 404 /404.php

# Force UTF-8
AddDefaultCharset utf-8

I wrote about this 如果您有兴趣,请在 CodePen 上发表博客文章。

HTML BP 有一个疯狂的 700+ 行 .htaccess,你可以看到一些很酷的技巧。

【讨论】:

  • 为什么是IfModule 的一部分而部分不是?
  • 我猜你可以把它全部放进去,IfModule,我只是没有。
  • 那部分在IfModule 中的原因是因为我从HTML BP 中提取了那部分(在IfModules 中包含所有内容),而我写了另一部分。而且我从未更改过它,我一直使用相同的.htaccess
  • 这很好用,谢谢,它也可以让你转到页面的 .php 版本,尽管我可以使用规范来解决这个问题。我认为这是最好的方法?
  • 是的,出于 SEO 原因,提供规范的元标记始终是一个好主意,并且 .php 版本将始终存在,因为这是实际文件的名称(并且不允许人们访问 .php版本没有多大意义。此外,这是允许两者的约定,请查看 www.facebook.com/index.php 和 www.facebook.com)。您只需要链接到非 .php 版本,例如"about.php" -> "about" 如果你想使用它。
猜你喜欢
  • 2011-03-02
  • 2017-07-14
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
相关资源
最近更新 更多