2012-03-19 74 views
3

我希望用户能够使用$_SERVER['PATH_INFO']作为一个占位符的文件服务,并在地址栏htaccess的重定向删除的index.php

比如我想为me.com/index.php/settings1me.com/settings1摆脱的index.php对于用户转到的任何PATH_INFO等等。

我该如何将它写成htaccess规则?我甚至不知道从哪里开始

如果有帮助,我参与了hostgator的共享托管计划。

基于@EddyFreddy的建议,我试了两种方式提到的问题,没有运气。下面是该文件的样子至今:

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 `?` 
+3

看到这个[问题] [1] [1]:http://stackoverflow.com/questions/4365129/htaccess-remove-index-php-from-url – 2012-03-19 03:58:54

+0

@EddyFreddy:它不工作,请参阅编辑 – qwertymk 2012-03-19 10:55:07

+0

@EddyFreddy:经过进一步检查,我发现我不需要*在地址栏中有'index.php',但我也希望它在那里被删除。例如'me.com/settings1'可以工作,但我也希望将'me.com/index.php/settings1'重定向到'me.com/settings1' – qwertymk 2012-03-19 11:02:31

回答

8

这可以很容易地通过mod_rewrite处理。在确认一切正常,然后更改RR=301

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] 

:下DOCUMENT_ROOT使用在.htaccess这个代码。

+0

对我来说这是有效的,但'http:// localhost/index.php'没有重定向到'http:// localhost /'。 – 2012-03-20 13:34:01

+0

@EddyFreddy:我不确定OP是否想重定向'http:// localhost/index.php',但是现在我已经做了一个编辑来处理这个边界情况。 – anubhava 2012-03-20 13:39:02

+0

令人惊叹的是,我总是想知道可以用神秘表达方式完成的疯狂事情:-) – 2012-03-20 14:17:23

1

长期评估之后我发现,从anubhava版本并非在所有情况下,为我工作。所以我尝试了这个修改版本。它将正确处理现有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] 
+0

我应该关心的是,人们可能会链接到index.php它会搅乱我的排名? – qwertymk 2012-03-19 21:23:23

+0

我会忽略'index.php/settings1'重定向到'/ settings1' - 这只有在你想将旧的PageRank解救到新的URL时才有意义。这似乎相当困难。 也许你应该看看不同的OSS,看看他们是如何做到的。 – 2012-03-20 03:34:08

+0

@qwertymk您是否使用类似codeignitter或zend的框架,或者您是否想构建自己的解决方案? – 2012-03-20 11:58:22

相关问题