简体   繁体   English

清洁网址www。 非,删除index.php,删除.php扩展名,使用HTACCESS添加结尾斜杠

[英]Clean URLS www. to non, remove index.php, remove .php extention, add trailing slash using HTACCESS

So it seems I can find solutions for some of these but can't get them to all work together. 因此,似乎可以找到其中一些解决方案,但无法使它们一起使用。 What I am trying to do is create clean URLS from all sides. 我想做的是从各个方面创建干净的URL。

  1. resolve all www. 解决所有www。 and non-www. 和非www。 to the non www. 到非www。 page
  2. remove all occurrences of index.php (ie if navigating to folder /blog/index.php resolve as /blog/) 删除所有出现的index.php(即,如果导航到文件夹/blog/index.php则解析为/ blog /)
  3. remove php extension from all URLS (ie /page.php to /page/) 从所有URL删除php扩展名(即/page.php到/ page /)
  4. add trailing slash (ie /page to /page/) 在末尾添加斜杠(即/ page到/ page /)

This is what I have so far: 这是我到目前为止的内容:

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

This accomplishes the clean URL's removing the .php extension and adding the trailing slash. 这样就完成了清除URL的工作,删除了.php扩展名并添加了斜杠。 I had to take out the www.to non and removal of index.php because the clean urls and trailing slashes stopped working. 我必须删除www.to non并删除index.php,因为干净的URL和斜杠停止了工作。 Thank you all in advance. 谢谢大家。

Here's what your .htaccess should look like: 这是您的.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 is a really good reference article about removing file extensions fro URLs. 是一篇非常不错的参考文章,关于删除URL的文件扩展名。 Just remember, for this to work, you must reference the non-extension version in all of your links eg <a href="about">About</a> , not <a href="about.php">About</a> 请记住,要使其正常工作,您必须在所有链接中引用非扩展版本,例如<a href="about">About</a> ,而不是<a href="about.php">About</a>

While you're doing .htaccess things, I might also recommend adding in the following snippets. 在执行.htaccess ,我可能还建议添加以下代码段。 The first two are concerned with website speed, the second is for a custom 404 page, and the third is for forcing UTF-8 (so you don't have to declare it in your HTML). 前两个与网站速度有关,第二个与自定义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 in a blog post on CodePen, if you're interested. 如果您有兴趣, 在CodePen上的博客文章中对此进行了介绍。

HTML BP has an insane 700+ line .htaccess that you can see for some cool tricks. HTML BP有700多个疯狂的.htaccess行,您可以看到一些很酷的技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM