问题描述
我使用的是Kohana的框架(但我认为这是不相关这一问题)和页面可以像这样访问
http://www.example.com/articles/
http://www.example.com/index.php/articles/
现在,作为一个经验法则,我一般尽量调整我的.htaccess只允许在一个页面的一种方式,默默地重定向等常用的方法。
基本上,在上面的第一URL时,地址实际上内部重定向到第二示例
我想要做的,是迫使第二类型的任何网址变成第一类型的URL。我不是经常有信心在.htaccess,和我第一次尝试抛出一些意想不到的结果(如有时无限循环)
下面是我已经想出了
重写规则^指数\ .PHP /(.*)$ 1 [NC,L,R = 301]
谁能告诉我我在做什么错了,如果你也遇到了这个问题,您怎么会解决它?
修改
我决定后我整个的.htaccess所以我所有的重定向可以参考一下。
#打开URL重写
RewriteEngine叙述上
#安装目录
的RewriteBase /〜toberua /
没有找到#文件页面
ErrorDocument的404/404 /
ErrorDocument的403/403 /
#让人们从我的目录
选项-Indexes
#默认页面加载
的DirectoryIndex index.php文件
#加斜杠,如果缺失
重写规则^(([A-Z0-9 \ - ] + /)* [A-Z0-9 \ - ] +)$ $ 1 / [NC,R = 301,L]
#重定向/favicon.ico请求
的RewriteCond%{REQUEST_URI}!^ /图片/设计/图标\ .ICO [NC]
的RewriteCond%{REQUEST_URI}图标\。(GIF | ICO | PNG |?JPE G)[NC]
重写规则(。*)图片/设计/ favicon.ico的[R = 301,L]
#发送/首页返回TLD
重写规则家用/ $ 1 [NC,R = 301,L]
#确保没有在地址栏中没有的index.php
的RewriteCond%{THE_REQUEST} ^ [AZ] {3,9} \ /.*指数\ .PHP \ HTTP /
重写规则^(。*)指标\ .PHP $ $ 1 [R = 301,L]#这是我企图阻止/dir/index.php并使其简单/ DIR /
重写规则^指数\ .PHP /(.*)$ 1 [NS,NC,L,R = 301]
#被查看保护应用程序和系统文件
重写规则^(应用|模块|系统) - [F,L]
#允许存在直接显示的任何文件或目录
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
#重写所有其他URL的index.php / URL
重写规则。*的index.php / $ 0 PT,L]
试试这个:
重写规则^指数\ .PHP /(.*)$ 1 [NS,NC,L,R = 301]
有什么你不处理是的所有的要求需重写,当你做一个重写它生成一个子请求 - 这也是受到改写。所以,你风重写/文章/index.php/articles,然后在子请求,这个问题,你重写/index.php/articles到/文章,产生了301重定向一个新的请求,和。添加NS标志将使这个规则不是子请求,我认为应该解决您的问题,除非你也做的/文章301运行 - > /index.php/articles重写(但是这将是疯狂)。
I am using the Kohana framework (but I think it's irrelevant for this question) and pages can be accessed like so
http://www.example.com/articles/
http://www.example.com/index.php/articles/
Now, as a rule of thumb, I generally try and tweak my .htaccess to only allow one way in for a page, and silently redirect other common ways.
Essentially, in the 1st URL above, the address is actually internally redirected to the 2nd example.
What I want to do, is to force any URLs of the 2nd type to turn into URLs of the first type. I'm not often confident in .htaccess, and my first attempt is throwing some unexpected results (like endless loops on occasion)
Here is what I've came up with
RewriteRule ^index\.php/(.*) $1 [NC,L,R=301]
Can anyone tell me what I'm doing wrong, and if you've also ran into this problem, how have you solved it?
EDIT
I've decided to post my entire .htaccess so all of my redirects can be examined.
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /~toberua/
# file not found page
ErrorDocument 404 /404/
ErrorDocument 403 /403/
# get people out of my directories
Options -Indexes
# default page to load
DirectoryIndex index.php
# add trailing slash if missing
RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]
# redirect /favicon.ico requests
RewriteCond %{REQUEST_URI} !^/images/layout/favicon\.ico [NC]
RewriteCond %{REQUEST_URI} favicon\.(gif|ico|png|jpe?g) [NC]
RewriteRule (.*) images/layout/favicon.ico [R=301,L]
# send /home back to TLD
RewriteRule home/ $1 [NC,R=301,L]
# ensure there is no /index.php in the address bar
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ $1 [R=301,L] # this was my attempt to stop /dir/index.php and make it simply /dir/
RewriteRule ^index\.php/(.*) $1 [NS,NC,L,R=301]
# Protect application and system files from being viewed
RewriteRule ^(application|modules|system) - [F,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]
Try this:
RewriteRule ^index\.php/(.*) $1 [NS,NC,L,R=301]
What you're not processing is that all requests are subject to rewriting, and when you do a rewrite it generate a subrequest -- which is also subject to rewriting. So you wind up rewriting /articles to /index.php/articles, then in the subrequest that that issue, you rewrite /index.php/articles to /articles and generate a new request for the 301 redirect, on and on. Adding the NS flag will make this rule not operate on subrequests, which I think should fix your problem, unless you're also doing a 301 on the /articles -> /index.php/articles rewrite (but that would be madness).
这篇关于从网站的URL删除的index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!