从网站 URL 中删除 index.php [英] Removing index.php from website URLs

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

问题描述

我正在使用 Kohana 框架(但我认为它与这个问题无关)并且可以像这样访问页面

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/

现在,根据经验,我通常会尝试调整我的 .htaccess 以仅允许页面的一种方式,并静默重定向其他常用方式.

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.

本质上,在上面的第一个 URL 中,地址实际上是在内部重定向到第二个示例.

Essentially, in the 1st URL above, the address is actually internally redirected to the 2nd example.

我想要做的是强制任何第二种类型的 URL 变成第一种类型的 URL.我对 .htaccess 并不经常有信心,我的第一次尝试是抛出一些意想不到的结果(有时像无限循环)

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)

这是我想出来的

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?

编辑

我决定发布我的整个 .htaccess 以便可以检查我的所有重定向.

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]

推荐答案

试试这个:

RewriteRule ^index.php/(.*) $1 [NS,NC,L,R=301]

您没有处理的是所有请求都可以重写,当您进行重写时,它会生成一个子请求——它也可以重写.因此,您最终将/articles 重写为/index.php/articles,然后在该问题的子请求中,将/index.php/articles 重写为/articles 并为 301 重定向生成一个新请求,如此反复.添加 NS 标志将使此规则不会对子请求进行操作,我认为这应该可以解决您的问题,除非您还在/articles ->/index.php/articles 重写上执行 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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