简体   繁体   English

使用.htaccess从网址中删除“ index.php”

[英]Remove 'index.php' from URL with .htaccess

I've been trying all sorts of solutions from this site and none seem to work. 我一直在尝试从该站点进行的各种解决方案,但是似乎都没有用。 I'm currently hosting with hostgator. 我目前正在与hostgator托管。 This is my current .htaccess file: 这是我当前的.htaccess文件:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule mod_suphp.c>
    suPHP_ConfigPath /home/user/php.ini
    <Files php.ini>
        order allow,deny
        deny from all

    </Files>
</IfModule>

This is in the root folder of my site. 这是在我网站的根文件夹中。 I have also tried adding a ? 我也尝试添加一个? after index.php and no luck. index.php之后,没有运气。 Does anyone know why this isn't working? 有人知道为什么这行不通吗?

This is the code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI: 这是您可以在.htaccess中使用的代码(在DOCUMENT_ROOT下),用于从URI中删除index.php:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

Symfony 2 has an excellent solution: Symfony 2具有出色的解决方案:

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]


RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]

RewriteRule .? %{ENV:BASE}/index.php [L]

This accomplishes the following: 这样可以完成以下任务:

  • RewriteBase is not necessary (useful when the site is in a subdirectory beneath the web root) RewriteBase (当站点位于Web根目录下的子目录中时很有用)
  • index.php is removed if present 如果存在,则删除index.php
  • The request is routed to the correct index.php with the full query string from the original request 使用原始请求的完整查询字符串将请求路由到正确的index.php

Note that the line: 注意这一行:

RewriteRule ^(.*) - [E=BASE:%1]

is responsible for setting the %{ENV:BASE} variable for later on. 负责为以后设置%{ENV:BASE}变量。 Refer to Apache documentation on E|env flag . 请参阅有关E|env标志的Apache文档

I tried this and works fine: 我尝试了这个并且工作正常:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Removes index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

    # Directs all EE web requests through the site index file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

Exceptions 例外情况

If your site's system directory (/system/) has been renamed and is still accessible by URL, modify the RewriteCond line above: 如果您站点的系统目录(/ system /)已重命名并且仍然可以通过URL访问,请修改上面的RewriteCond行:

RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]

If you are running EE from a sub-directory rather from the root of your domain (eg http://example.com/myeesite/ instead of http://example.com/ ), just remove the slash preceding index.php in the RewriteRule line above, like so: 如果您从子目录而不是域的根目录(例如, http : //example.com/myeesite/而不是http://example.com/ )运行EE,则只需在索引中删除index.php之前的斜杠即可。上面的RewriteRule行,如下所示:

RewriteRule ^(.*)$ index.php/$1 [L]

If you are running EE from a sub-directory and it still doesn't work after removing the slash, you may need to specify the sub-directory in your rewrite rule. 如果您正在从子目录运行EE,但在删除斜杠后仍无法使用EE,则可能需要在重写规则中指定子目录。 For example, if your sub-folder is named testing, change: 例如,如果您的子文件夹名为test,请更改:

RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

To: 至:

RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]

And change: 并更改:

RewriteRule ^(.*)$ /index.php/$1 [L]

To: 至:

RewriteRule ^(.*)$ testing/index.php/$1 [L]

If your host requires forcing query strings, try adding a question mark following index.php in the RewriteRule line above, like so:

RewriteRule ^(.*)$ /index.php?/$1 [L]

To remove index.php from urls on apache2.4 you can use the following rule : 要从apache2.4上的网址中删除index.php ,可以使用以下规则:

 RewriteEngine on

 RewriteRule ^index\.php/(.+)$ /$1 [L,R]
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule !index\.php /index.php%{REQUEST_URI} [L,END]

This will change the uri 这将改变你的uri

/index.php/foobar

to

/foobar

This rule will return an internal server error for lower versions of apache as they don't support the END flag. 对于较低版本的apache,此规则将返回内部服务器错误,因为它们不支持END标志。 Please see the anubhava's answer above that works almost on all versions. 请查看上面适用于几乎所有版本的anubhava答案。

Updated Answer 更新的答案

These days I just use the Wordpress .htaccess as a base: it is relatively simple and works as expected: 这些天,我只使用Wordpress .htaccess作为基础:它相对简单,并且可以按预期工作:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

It's also straightforward to force https:// if you desire: 如果您愿意,也可以直接强制使用https://

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST} [L,R=301]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Original Post from 2012 2012年的原始帖子

My initial suggestion was to first do a hard redirect (to strip index.php ) and then an internal redirect back to index.php . 我最初的建议是先进行硬重定向(剥离index.php ),然后进行内部重定向回到index.php

The problem is that a greedy internal redirect match (eg using (.*) ) seems to trigger the hard redirect again, leading to an infinite redirect loop. 问题是贪婪的内部重定向匹配(例如使用(.*) )似乎再次触发了硬重定向,从而导致无限重定向循环。

I feel that there is a more elegant solution than what I'm about to suggest, but unfortunately I don't have the Apache know-how to think of it. 我觉得有一个比我建议的方案更优雅的解决方案,但是不幸的是,我没有Apache的专业知识来思考它。

What I would do is to have a RewriteRule that performs a hard redirect to strip out the index.php and then a set of internal redirect rules to take it from there. 我要做的是拥有一个RewriteRule ,它执行硬重定向以index.php ,然后执行一组内部重定向规则以从那里获取它。 For example: 例如:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php(.*)$ /$1 [R,L,QSA]
RewriteRule ^pie$ /index.php?pie [L,QSA]
</IfModule>

This does seem to work correctly without producing any 500 errors. 这似乎确实可以正常工作,而不会产生任何500个错误。 But again, I recommend a pinch of salt. 但是,我再次推荐一点盐。

While that's a far cry from a good solution, I hope it helps. 尽管这与好的解决方案相去甚远,但我希望它能有所帮助。

I've been compelled to join stack overflow.com today to comment here as the answer has solved a long term problem I've had with a Zend Framework website. 今天,我被迫加入stackoverflow.com在这里发表评论,因为答案已经解决了我在Zend Framework网站上遇到的长期问题。 I've worked on the website around 3 1/2 years and during that time I discovered that it didn't handle index.php correctly and this causes webmaster tools to see duplicate titles etc. 我在网站上工作了大约3 1/2年,在那段时间里,我发现它无法正确处理index.php,这导致网站站长工具看到重复的标题等。

I decided to search again today for a solution, one of many times attempted. 我决定今天再次尝试寻找解决方案,这是许多次尝试中的一次。

This is the one (the last answer shown above) that solves my problem. 这是解决我的问题的一个(上面显示的最后一个答案)。

    # Removes index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

As an example. 举个例子。 this is what it achieves for me. 这就是它为我实现的。

http://www.domainname.co.uk/index.php/categories/url-name.html http://www.domainname.co.uk/index.php/categories/url-name.html

becomes 变成

http://www.domainname.co.uk/categories/url-name.html http://www.domainname.co.uk/categories/url-name.html

Thank you to everyone who contributed to the original question as it lead to the answer and solution above. 感谢所有对原始问题做出贡献的人,因为它导致了上面的答案和解决方案。

Extra Note: I have other rewrite commands that handles the other aspects but those on their own didn't fix the index.php and the only time this has been fixed is by using. 额外注意:我还有其他重写命令可以处理其他方面,但是它们本身并不能修复index.php,并且只有通过使用才能解决此问题。

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

I only hope it helps others whether its ExpressionEngine or Zend Framework, in the future. 我只希望将来对它的ExpressionEngine或Zend Framework有所帮助。

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

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