简体   繁体   English

htaccess重定向删除index.php

[英]htaccess redirect to remove index.php

I want user to be able use $_SERVER['PATH_INFO'] as a placeholder for which file to serve and get rid of index.php in the address bar 我希望用户能够使用$_SERVER['PATH_INFO']作为占位符,为哪个文件提供服务并摆脱地址栏中的index.php

For example I want to serve me.com/index.php/settings1 as me.com/settings1 and so on for any PATH_INFO that the user goes to. 例如,我想将me.com/index.php/settings1作为me.com/settings1提供给用户使用的任何PATH_INFO等等。

How would I write this as a htaccess rule? 我怎么写这个作为htaccess规则? I don't even know where to start 我甚至不知道从哪里开始

If it helps, I'm on a shared hosting plan from hostgator. 如果它有帮助,我正在使用hostgator的共享主机方案。

Based on @EddyFreddy suggestion I tried both ways mentioned in that question with no luck. 根据@EddyFreddy的建议,我尝试了在这个问题中提到的两种方法,没有运气。 Here's what the file looks like so far: 这是目前文件的样子:

suPHP_ConfigPath /home/user/php.ini
    <Files php.ini>
        order allow,deny    
        deny from all   
    </Files>

RewriteEngine On
RewriteBase /   

RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC]
RewriteRule .? http://mysite.com%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L] # tried with and without the `?`

This can be easily handled by mod_rewrite. 这可以通过mod_rewrite轻松处理。 Use this code in .htaccess under DOCUMENT_ROOT: 在DOCUMENT_ROOT下的.htaccess中使用此代码:

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

# external redirect from /index.php/foo to /foo/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php(/[^\s\?]+)? [NC]
RewriteRule ^ %1/ [L,R]

# external redirect to add trailing slash
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+((?!.+/[\s\?])[^\s\?]+) [NC]
RewriteRule ^ %1/ [L,R]

# internal forward from /foo to /index.php/foo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php%{REQUEST_URI} [L,NC]

After you verify that everything is working fine then change R to R=301 . 验证一切正常后,将R更改为R=301

After longterm evaluation I found out, that the version from anubhava doesn't work for me in all cases. 经过长期评估后,我发现anubhava的版本在所有情况下对我都不起作用。 So I tried this modified version. 所以我试过这个修改过的版本。 It will handle existing files in existing dirs correct and produces no double-slashes. 它将处理现有dirs中的现有文件,并且不会产生双斜线。

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

###### Add trailing slash (optional) ######
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ $1/ [R=301,L]

###### external redirect from /index.php/foo to /foo ######
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php(/.+)?[\s\?] [NC]
RewriteRule ^ %1 [L,R=301]

###### internal forward from /foo to /index.php/foo ######
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php%{REQUEST_URI} [L]

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

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