【问题标题】:htaccess redirect to remove index.phphtaccess 重定向以删除 index.php
【发布时间】:2012-04-03 14:57:11
【问题描述】:

我希望用户能够使用$_SERVER['PATH_INFO'] 作为要提供的文件的占位符并删除地址栏中的 index.php

例如,我想为用户访问的任何 PATH_INFO 提供 me.com/index.php/settings1me.com/settings1 等等。

如何将其写为 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 `?`

【问题讨论】:

  • 看到这个[问题][1] [1]:stackoverflow.com/questions/4365129/…
  • @EddyFreddy:它不工作查看编辑
  • @EddyFreddy:经过进一步检查,我发现我不需要在地址栏中有index.php,但如果是,我也希望将其删除那里。例如me.com/settings1 有效,但我也希望me.com/index.php/settings1 重定向到me.com/settings1

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

经过长期评估,我发现来自 anubhava 的版本并非在所有情况下都适合我。所以我尝试了这个修改后的版本。它将正确处理现有目录中的现有文件,并且不会产生双斜杠。

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]

【讨论】:

  • 我是否应该足够在意人们可能会链接到 index.php,以免影响我的排名?
  • 我会忽略 index.php/settings1redirecting 到 /settings1 - 这只有在你想将旧的 Pagerank 拯救到新的 URL 时才有意义。这似乎相当困难。也许你应该看看不同的 OSS,看看他们是如何做到的。
  • @qwertymk 您是在使用 codeignitter 或 zend 之类的框架,还是要构建自己的解决方案?
  • @qwertymk 这既快又脏。我建议您使用 Zend 或 Codeignitter 之类的框架 - 它们在快速构建网站和应用程序方面做得很好,并且有很好的使用 ajax 的方法。
【解决方案2】:

这可以通过 mod_rewrite 轻松处理。在 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]

确认一切正常后,将R 更改为R=301

【讨论】:

  • 对我来说这是可行的,但 http://localhost/index.php 不会重定向到 http://localhost/
  • @EddyFreddy:我不确定 OP 是否也想重定向 http://localhost/index.php,但现在我已经进行了编辑,也将处理这种边缘情况。
  • 太棒了,我总是想知道用神秘的表达方式可以完成哪些疯狂的事情:-)
  • 您是否有可能将外部重定向的规则集与我的 2. 中的“添加尾部斜杠如 WP”部分结合起来。编辑为一个规则?您能否在帖子的另一个代码部分中添加一个带有斜杠的解决方案(与 %{QUERY_STRING} 兼容,意味着 /path?var= 到 /path/?var=)?这对所有人来说都是最伟大的事情!
  • @EddyFreddy:请检查我上面编辑的答案。现在,这些重定向将始终添加尾部斜杠/,无论是否带有查询字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多