如何配置.htaccess文件的重写规则摆脱扩展,尾随斜线,WWW? [英] How to configure .htaccess file for rewrite rules to get rid of extensions, trailing slashes, www?

查看:159
本文介绍了如何配置.htaccess文件的重写规则摆脱扩展,尾随斜线,WWW?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要的 -

This is what I want -

当有人进入site.com/page.php或site.com/page或site.com/page/,都应该工作相同,并在地址栏中显示的URL应该site.com/page [我不喜欢的扩展和结尾的斜杠]

When somebody enters site.com/page.php or site.com/page or site.com/page/ , all should work the same, and the url displayed in the address bar should be site.com/page [ I don't like extensions and trailing slashes ]

我也想摆脱从地址栏的WWW的,因为它是不必要的。

I also want to get rid of the www from the url bar, as it's unnecessary.

这是我目前的.htaccess文件,重写规则 -

This is my current .htaccess files Rewrite rules -

RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.com
RewriteRule ^(.*)$ http://www.site.com/$1 [R=permanent,L] 

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

这解决了www和扩展的问题,而不是结尾的斜线的!

This solves the www and the extension issue, but not the trailing slash one!

但是,更重要的是,我最近安装了Word preSS在site.com/blog,但每当我尝试访问,我重定向到404页面代替。它的工作原理,当我删除所有的修改可以!

But, more importantly, I recently installed WordPress at site.com/blog, but whenever I try to access that, I am redirected to the 404 page instead. It works when I remove all the Rewrites!

有我搞砸了我重写?我不是一个普通的前pressions忍者!

Have I messed up my rewrites? I am not a regular expressions ninja!

推荐答案

尝试添加以下到现有的.htaccess文件的顶部,上面的任何现有的规则,如:字preSS规则。

Try adding the following to the top of your existing .htaccess file, above any existing rules e.g. Wordpress rules.

RewriteEngine on
RewriteBase /

#if the host is not site.com, e.g www.site.com
RewriteCond %{HTTP_HOST} !^site\.com$ [NC]
#then redirect to site.com e.g to remove the www
RewriteRule ^(.*)$ http://site.com/$1 [R=301,L] 

#if the URL ends with a slash
RewriteCond %{REQUEST_URI} ^/(.+)/$
#redirect to URL without slash
RewriteRule . http://site.com/%1 [R=301,L] 

#skip wordpress site which starts with blog
RewriteCond %{REQUEST_URI} !^/blog [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

这篇关于如何配置.htaccess文件的重写规则摆脱扩展,尾随斜线,WWW?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆