删除网址的index.php文件和结尾的斜杠 [英] Remove index.php AND trailing slashes from URL

查看:327
本文介绍了删除网址的index.php文件和结尾的斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的.htaccess以下重写规则,第4条线路都应该处理,允许访问该网站,而index.php文件,并能正常工作,直到我添加了最后一点,我正在努力用来删除该网站的网址结尾的斜杠。

 的RewriteCond%{REQUEST_URI}!(\。[A-ZA-Z0-9] {1,5})$
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond $ l ^(上传|缓存|专题|中|管理员\ .PHP |图标\ .ICO |机器人\ .TXT |指数\ .PHP)![NC]
重写规则^(。*)$ /index.php/$1

#删除结尾的斜杠
重写规则^(+)/ $ HTTP://%{HTTP_HOST} / $ 1 [R = 301,L]
 

当我添加了最后一行,我访问我的网站的根目录下,在index.php部分被附加到URL,这是为什么?

解决方案
  

当我添加了最后一行,我访问我的网站的根目录下,在index.php部分被附加到URL,这是为什么?

这是因为规则顺序施加。你想重定向之前的你路线的东西的index.php 来发生的。只要将身边的规则:

 #删除尾随斜线
重写规则^(+)/ $ HTTP://%{HTTP_HOST} / $ 1 [R = 301,L]

的RewriteCond%{REQUEST_URI}!(\。[A-ZA-Z0-9] {1,5})$
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond $ l ^(上传|缓存|专题|中|管理员\ .PHP |图标\ .ICO |机器人\ .TXT |指数\ .PHP)![NC]
重写规则^(。*)$ /index.php/$1 [L]
 

I've got the following rewrite rules in my .htaccess, the first 4 lines are supposed to deal with allowing to access the site without index.php, and works fine, until I add the last bit which I'm trying to use to remove trailing slashes from the sites URLs.

RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(uploads|cache|themes|default|admin\.php|favicon\.ico|robots\.txt|index\.php) [NC]
RewriteRule ^(.*)$ /index.php/$1

# Remove trailing slashes
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

When I add the last line, and I visit the root of my site, the index.php part gets appended to the URL, why is this?

解决方案

When I add the last line, and I visit the root of my site, the index.php part gets appended to the URL, why is this?

This is because the rules are applied sequentially. You want the redirect to happen before you route stuff to /index.php. Just swap those rules around:

# Remove trailing slashes
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(uploads|cache|themes|default|admin\.php|favicon\.ico|robots\.txt|index\.php) [NC]
RewriteRule ^(.*)$ /index.php/$1  [L]

这篇关于删除网址的index.php文件和结尾的斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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